即使有其他输入元素也隐式提交表单
Form submitted implicitly even when there are other input element
基于HTML specs:
4.10.21.2 Implicit submission
A form element's default button is the first submit button in tree order whose form owner is that form element.
If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key
while a text control is focused implicitly submits the form), then
doing so for a form, whose default button has activation behavior and
is not disabled, must cause the user agent to fire a click event at
that default button.
There are pages on the web that are only usable if there is a way to implicitly submit forms, so user agents are strongly encouraged to
support this.
If the form has no submit button, then the implicit submission mechanism must do nothing if the form has more than one field that
blocks implicit submission, and must submit the form element from the
form element itself otherwise.
我的期望是当一个表单有多个输入元素(并且没有提交按钮)时,当用户点击回车按钮时表单不应该被隐式提交。
我有一个带有按钮(不是提交按钮)和多个输入元素的表单。
<form id="myForm" method="post" action="/dosomething">
<input id="validCategory0" name="validCategory" type="radio" value="Yes">Yes
<input id="validCategory1" name="validCategory" type="radio" value="No">No
<br/>
<input id="itemid" type="hidden" />
<br/> First Name
<input id="fn" width="200" />
<br/>
<button type="button">Submit</button>
</form>
对于上面的 HTML,当光标位于名字文本框内并且用户按下回车键时,表单将被提交。当表单中有多个输入元素时,我不确定为什么要提交表单。
我正在使用Chrome
我的观察:如果我如下更改 hidden
输入元素,则不会隐式提交表单。
来自
<input id="itemid" type="hidden" />
至
<input id="itemid" hidden />
正如我在评论中所说,您忘记添加引文部分:
"For the purpose of the previous paragraph, an element is a field that
blocks implicit submission of a form element if it is an input element
whose form owner is that form element and whose type attribute is in
one of the following states: Text, Search, URL, Telephone, E-mail,
Password, Local Date and Time, Date, Month, Week, Time, Number"
因此单选按钮、复选框和隐藏类型等输入不在该列表中,因此它们不适用。当您有第二个输入时,例如 <input id="itemid" type="hidden" />
您明确地将类型设置为隐藏,因此它不会阻止提交。删除类型,使 <input id="itemid" />
确实 停止隐式提交,因为输入的默认类型是 text
,它在该列表中,因此它停止隐式提交.同样,当你将输入切换为 <input id="itemid" hidden />
时,类型仍然是 text
,因此它也停止了隐式提交。
基于HTML specs:
4.10.21.2 Implicit submission
A form element's default button is the first submit button in tree order whose form owner is that form element.
If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text control is focused implicitly submits the form), then doing so for a form, whose default button has activation behavior and is not disabled, must cause the user agent to fire a click event at that default button.
There are pages on the web that are only usable if there is a way to implicitly submit forms, so user agents are strongly encouraged to support this.
If the form has no submit button, then the implicit submission mechanism must do nothing if the form has more than one field that blocks implicit submission, and must submit the form element from the form element itself otherwise.
我的期望是当一个表单有多个输入元素(并且没有提交按钮)时,当用户点击回车按钮时表单不应该被隐式提交。
我有一个带有按钮(不是提交按钮)和多个输入元素的表单。
<form id="myForm" method="post" action="/dosomething">
<input id="validCategory0" name="validCategory" type="radio" value="Yes">Yes
<input id="validCategory1" name="validCategory" type="radio" value="No">No
<br/>
<input id="itemid" type="hidden" />
<br/> First Name
<input id="fn" width="200" />
<br/>
<button type="button">Submit</button>
</form>
对于上面的 HTML,当光标位于名字文本框内并且用户按下回车键时,表单将被提交。当表单中有多个输入元素时,我不确定为什么要提交表单。
我正在使用Chrome
我的观察:如果我如下更改 hidden
输入元素,则不会隐式提交表单。
来自
<input id="itemid" type="hidden" />
至
<input id="itemid" hidden />
正如我在评论中所说,您忘记添加引文部分:
"For the purpose of the previous paragraph, an element is a field that blocks implicit submission of a form element if it is an input element whose form owner is that form element and whose type attribute is in one of the following states: Text, Search, URL, Telephone, E-mail, Password, Local Date and Time, Date, Month, Week, Time, Number"
因此单选按钮、复选框和隐藏类型等输入不在该列表中,因此它们不适用。当您有第二个输入时,例如 <input id="itemid" type="hidden" />
您明确地将类型设置为隐藏,因此它不会阻止提交。删除类型,使 <input id="itemid" />
确实 停止隐式提交,因为输入的默认类型是 text
,它在该列表中,因此它停止隐式提交.同样,当你将输入切换为 <input id="itemid" hidden />
时,类型仍然是 text
,因此它也停止了隐式提交。