输入时提交的表单至少需要一个输入类型 = "text" 元素
Form submit on enter needs at-least one input type = "text" element
我观察到表单元素应该至少需要一个 input type = "text" 元素才能在按下回车键时提交表单。
这是我的代码:
<form ng-submit="executeFunc()">
<input type="text"/>
<input type="submit"/>
</form>
但是如果我们没有第一个输入类型 ="text" 元素,而是有单选按钮或 select 框、复选框怎么办。
如果是这种情况,那么如何在输入时提交表单。
我想知道是否符合 W3C 标准,我们需要提供至少一种输入类型 "text" 元素?
每次按下 enter
键时,只需在提交按钮上触发 click
事件。
$(document).keypress(function (e) {
if (e.keyCode === 13) {
$('input[type="submit"]').click();
}
});
关于添加的问题“根据W3C标准,我们需要提供至少一种输入类型"text"元素?”,答案是没有HTML规范要求 表单可以通过按 Enter 键提交。规范只是(模糊地)描述了浏览器可以做什么。
最初的想法是,您可以在填写输入字段后按 Enter 键来提交仅包含一个文本输入字段(可能还有单选按钮和复选框)的表单。这被认为对简单的表单很有用,通常是搜索表单或订阅表单,可以让用户更快地完成操作。
后来,这个想法经常扩展到具有多个文本输入字段的表单。这通常会导致问题,因为用户可能会在填写他应该填写的所有字段之前按下 Enter。然而,这就是现代浏览器通常的行为方式(前提是表单中有提交按钮)。
HTML5 遵循传统,只是描述浏览器可能会做什么,尽管通过强烈推荐(但不要求符合)常见行为而偏离传统,在 Implicit submission 部分:
If the user agent supports letting the user submit a form implicitly
(for example, on some platforms hitting the "enter" key while a text
field is focused implicitly submits the form), then doing so for a
form whose default button has a defined activation behavior must cause
the user agent to run synthetic click activation steps on that default
button.
[...]
Note: 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
(关于原始问题,答案是你需要在 JavaScript 中处理这些事情,@bardznusny 的答案显示了一种简洁的方法。)
我观察到表单元素应该至少需要一个 input type = "text" 元素才能在按下回车键时提交表单。
这是我的代码:
<form ng-submit="executeFunc()">
<input type="text"/>
<input type="submit"/>
</form>
但是如果我们没有第一个输入类型 ="text" 元素,而是有单选按钮或 select 框、复选框怎么办。 如果是这种情况,那么如何在输入时提交表单。
我想知道是否符合 W3C 标准,我们需要提供至少一种输入类型 "text" 元素?
每次按下 enter
键时,只需在提交按钮上触发 click
事件。
$(document).keypress(function (e) {
if (e.keyCode === 13) {
$('input[type="submit"]').click();
}
});
关于添加的问题“根据W3C标准,我们需要提供至少一种输入类型"text"元素?”,答案是没有HTML规范要求 表单可以通过按 Enter 键提交。规范只是(模糊地)描述了浏览器可以做什么。
最初的想法是,您可以在填写输入字段后按 Enter 键来提交仅包含一个文本输入字段(可能还有单选按钮和复选框)的表单。这被认为对简单的表单很有用,通常是搜索表单或订阅表单,可以让用户更快地完成操作。
后来,这个想法经常扩展到具有多个文本输入字段的表单。这通常会导致问题,因为用户可能会在填写他应该填写的所有字段之前按下 Enter。然而,这就是现代浏览器通常的行为方式(前提是表单中有提交按钮)。
HTML5 遵循传统,只是描述浏览器可能会做什么,尽管通过强烈推荐(但不要求符合)常见行为而偏离传统,在 Implicit submission 部分:
If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text field is focused implicitly submits the form), then doing so for a form whose default button has a defined activation behavior must cause the user agent to run synthetic click activation steps on that default button.
[...]
Note: 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
(关于原始问题,答案是你需要在 JavaScript 中处理这些事情,@bardznusny 的答案显示了一种简洁的方法。)