为什么在输入中按回车键提交具有单个输入字段的表单

为什么在输入中按回车键提交具有单个输入字段的表单

问题描述:

为什么带有单个 字段的

会在用户输入值并按下 时重新加载表单输入,如果
?中有2个或更多字段,则不会.

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>

这是一个鲜为人知的怪癖",已经有一段时间了.我知道有些人已经通过各种方式解决了这个问题.

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.