为什么在输入时按下Enter键提交单个输入字段的表单?

为什么在输入时按下Enter键提交单个输入字段的表单?

问题描述:

为什么只有一个< input> 字段的< form> 会重新加载表单当用户输入一个值并按下 Enter 时,如果< form> ?中有两个或更多个字段,则不会。

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.

如果您在第二种形式中输入一个值并按下Enter键,您会看到它会重新加载传递输入值的页面,就像您调用 GET 。为什么?如何避免它?

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>


这是一个鲜为人知的Quirk出去一会儿。我知道有些人已经以各种方式解决了问题。

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.

我应该注意到,我听说这个问题最常见的地方是IE特别是,而不是与FireFox或其他。尽管它似乎也影响它们。

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.