views:

1187

answers:

6

Why is it that a form with a single input field will reload the form when the user enters a value and presses the Enter key, and it does not if there are 2 or more fields in the form. I wrote a simple page to test this oddity.

If you enter a value in the second form and press Enter, you'll see it reloads the page passing the entered value as if you called GET. why? and how do I avoid it?

http://testformenter.html?partid=123

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>testFormEnter</title>
</head>
<body>
<form>
  <input type="text" name="partid2" id="partid2" />
  <input type="text" name="partdesc" id="partdesc"  />
</form>
  <p>2 field form works fine</p>
<form>
<input type="text" name="partid" id="partid"  />
</form>
<p>One field form reloads page when you press the Enter key why</p>
</body>
</html>
+5  A: 

This is a little known "Quirk" that has been out for a while. I know some people have resolved it in various ways.

The easiest bypass in my opinion is to simply have a second input that isn't displayed to the user. Granted not all that user friendly on the backend, it does work to resolve the issue.

I should note that the most common place that I hear of this issue is with IE specifically and not with FireFox or others. Although it does seem to affect them as well.

Mitchel Sellers
Thanks, I wish it were documented somewhere. I wasted hours debugging a large page thinking it was something else, tied to my ajax. I only discovered the "quirk" after I extracted the code and started to delete line after line. What a waste of time!
sdfor
And you are right, it's an easy fix with a hidden field. Incidentally if affects firefox 3.5.2. Maybe it's considered a feature.
sdfor
Ah yes, I feel your pain....that is how I found out about it, say 5-6 years ago...
Mitchel Sellers
Also I discovered that a hidden field doesn't always work. On one page it did and another it didn't - also odd. but hiding the field with javascript did the trick.You need to write an article "Things you don't know that will eat up your time" - thanks again
sdfor
+1  A: 

It's not reloading the page as such, it's submitting the form.

However, in this example because you have no action attribute on the form it submits to itself which gives the impression of reloading the page.

Also, I can't repro the behaviour you describe. If I am in any text input in a form and I press Enter it submits the form, no matter where in the form the input is located or how many inputs there are.

You might want to try this out some more in different browsers.

Andy Hume
You're right I was not using the right words. It is submitting the form and since I don't specify an action it defaults to itself.I copied the exact HTML and ran it in IE8 and Firefox 3.5.2.An enter key in the first 2 fields, that are in the first form, do not submit the form, while an enter key in the third field does.
sdfor
+2  A: 

This is a known bug in IE6/7/8. It doesn't appear that you will get a fix for it.

The best workaround you can do for this, is to add another hidden field (if your engineering conscience permits). IE will no longer auto-submit a form when it finds that there are two input-type fields in the form.

Update

In case you were wondering why this is the case, this gem comes straight out of the HTML 2.0 specification (Section 8.2).

Vineet Reynolds
This also happens in Firefox 3.5
Dimitri Wetzel
+2  A: 

Pressing Enter works differently depending on (a) how many fields there are and (b) how many submit buttons there are. It may do nothing, it may submit the form with no 'successful' submit button, or it may pretend the first submit button was clicked (even generating an onclick for it!) and submit with that button's value.

For example, if you add an input type="submit" to your two-field form, you'll notice it too submits.

This is an ancient browser quirk going back at least as far as early Netscape (maybe further), which is unlikely to be changed now.

<form>

Invalid without an ‘action’. If you don't intend to submit anywhere, and you don't need radio button name grouping, you could just completely omit the form element.

bobince
thanks, good answer. Though I need the form to serialize the fields for Ajax. I could code it manually, but with many forms it would be a pain.
sdfor
A: 
sdfor
A: 

Here is the code that I used would use to solve the problem:

<form>
<input type="text" name="partid" id="partid"  />
<input type="text" name="StackOverflow1370021" value="Fix IE bug" style="{display:none}" />
</form>
JeffJak