Must say this is an odd request. It drives me absolutely nuts when
enter does NOT submit the form and I'm forced to reach for my mouse.
That said, it's simple: don't add a submit button (input
type="submit"). Use a link that uses javascript to submit the form.
Jason
RSL
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Submittable Buttonless Form</title>
</head>
<body>
<form action="foo">
<p>
<input type="text">
</p>
</form>
</body>
</html>
RSL
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Submittable Buttonless Form</title>
</head>
<body>
<form action="foo" method="post">
<div>
<input type="text" id="login" name="login"/>
</div>
<div>
<input type="password" name="password" name="login"/>
</div>
</form>
</body>
</html>
And this one, that does
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Submittable Buttonless Form</title>
</head>
<body>
<form action="foo" method="post">
<div>
<input type="text" id="login" name="login"/>
</div>
<div>
<input type="password" name="password" name="login"/>
</div>
<div>
<input type="submit"/>
</div>
</form>
</body>
</html>
So yeah, there's a combination of form fields that will not submit on
Enter. Sometimes it will work, sometimes it won't. If you want it to
work all the time, put in a submit tag. If you don't want it to happen
ever, you probably need extra javascript just to be safe.
Jason
Jason
RSL
Configure the submit button to set that value to true and then submit
the form.
-philip
Let's go over things...
You want a button that submits normally but want to try and stop the
enter key submitting it. To do this, you're going to need to understand
the relationship between the ENTER key, the Mousebutton and Javascript.
When you press ENTER in a form, the form automatically goes to the first
Submit button it can find (or usually the closest in accordance to
tabindexing) and then submits the form. It treats the ENTER key like it
was the left mouse button because it has to assume that some people
don't have a mouse (partially sighted, screen readers etc).
In Javascript, when you press ENTER in a form, it also relates this to
the left mouse button being clicked, which therefor activates the
"onclick" event. It also sets a keycode (13) so it knows what key on the
keyboard was pressed.
However, Javascript has another event called "onmousedown" which only
works if the mouse button is pressed down. It doesn't get related to the
keyboard or the ENTER key. I believe onkeydown is for that.
Now!, the onmousedown event is activated before the onclick method which
means we have a way of setting a rule before doing anything with
onclick. (Onclick doesn't activate until you let go of the clicked
button).
So, the easiest solution (if we don't do anything fancy with functions)
is to apply two javascript events to your submit button.
Example
==============
<input type="submit" name="mybutton" onmousedown="this.title=1"
onclick="if(this.title!=1){return false;}" value="Submit my Form" />
How it works?
==============
What happens is.. if you click the button, the onmousedown event runs
first which renames/sets the title of the button. When you let go of the
mousebutton, the onclick takes effect and checks the title to see if its
set to 1. If it is, then it submits, otherwise it returns false.
This means that, if you pressed ENTER, onmousedown wouldn't have renamed
the title which means the onclick will return false and not submit the
form.
Obviously, as I know the Mootools library I've made a transparent method
which just adds the two events to all input=submit buttons on the page.
The inline example here is just to show you the method for copying and
pasting quickly :)
Hope that helps someone!
Adam
www.greatbigmassive.net
--
Posted via http://www.ruby-forum.com/.