问题描述
我刚刚发现一个只包含一个 INPUT(未隐藏)的 FORM 会在按下 Enter 时自动提交.
I have just found out that a FORM containing only one INPUT (not hidden) will automatically submit when pressing Enter.
但是一个包含至少两个输入(非隐藏)的表单在按下回车键时不会提交.
But a form containing at least two INPUTS (not hidden) will NOT submit when pressing enter.
(所有脚本都没有内部提交/按钮/输入[type=submit])
(None of the scripts have a submit/button/input[type=submit] inside)
看看这个 jsfidle.这种行为有解释/标准吗?
Take a look at this jsfidle. Is there an explanation/standard for this behavior?
<form id="form1" method="POST">
<p>Does submit:</p>
<input type="text" placeholder="focus and press enter"/>
</form>
<form id="form2" method="POST">
<p>Does <strong>not</strong> submit:</p>
<input type="text" placeholder="does not submit"/>
<input type="text" placeholder=""/>
</form>
推荐答案
此行为是在 HTML 2.0 规范中引入的.有关详细信息,请参阅以下文章:
This behaviour was introduced in the HTML 2.0 specification. See the following article for more details:
当一个表单中只有一个单行文本输入框时,用户代理应该接受在该字段中输入作为提交请求表格
来源:W3C 规范
这篇关于为什么带有一个文本 INPUT 的 FORM 会在输入时提交,而带有两个文本 INPUT 的 FORM 不会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!