Why does forms with single input field submit upon

2019-01-01 15:40发布

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, 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?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>

13条回答
怪性笑人.
2楼-- · 2019-01-01 16:17

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.

查看更多
残风、尘缘若梦
3楼-- · 2019-01-01 16:18

Thanks to everyone who answered. It's an eye opener that a form with a single field acts differently then a form with many fields.

Another way to deal with this automatic submit, is to code a submit function that returns false.

In my case I had a button with an onclick event, so I moved the function call with the added return keyword to the onsubmit event. If the function called returns false the submit won't happen.

<form onsubmit="return ajaxMagic()">
<input type="text" name="partid" id="partid"  />
<input type="submit" value="Find Part" />
</form

function ajaxMagic() {
  ...
  return (false);
}
查看更多
皆成旧梦
4楼-- · 2019-01-01 16:23

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.

查看更多
君临天下
5楼-- · 2019-01-01 16:23

This problem occurs in both IE and Chrome. It does not occur on Firefox. A simple solution would be to add the following attribute to the form tag: onsubmit="return false"

That is, of course, assuming that you submit the form using an XMLHttpRequest object.

查看更多
皆成旧梦
6楼-- · 2019-01-01 16:24

No, the default behaviour is that on enter, last input in the form is submitted. If you don't want to submit at all you could add:

<form onsubmit="return false;">

Or in your input

<input ... onkeypress="return event.keyCode != 13;">

Of course there are more beautiful solutions but these are simpler without any library or framework.

查看更多
怪性笑人.
7楼-- · 2019-01-01 16:27

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>
查看更多
登录 后发表回答