简体   繁体   English

HTML 表单中“返回”键的默认行为是什么?

[英]What is the default behavior of the 'return' key in an HTML form?

I have always believed that the default behavior of hitting the 'Return' key was to submit the form.我一直认为点击“返回”键的默认行为是提交表单。 But it seems to be more complex: if there is a nearby button, the 'Return' key instead seems to simulate a 'click' event on that button, instead of submitting the form.但它似乎更复杂:如果附近有一个按钮,则“返回”键似乎模拟该按钮上的“单击”事件,而不是提交表单。 Here's an example:下面是一个例子:

 document.querySelector('form').onsubmit = ev => { ev.preventDefault(); document.querySelector('#result').innerText = ev.target.elements.text.value; }; document.querySelector('#troublesome').onclick = ev => { ev.preventDefault(); ev.target.innerText = Math.random(); };
 <form> <input type="text" name='text' value='something' /> <button id="troublesome">Trouble</button> <button>Submit</button> </form> <div id="result">not submitted</div>

Above, when you hit return in the <input> element, the browser chooses to click the "Troublesome" button.上面,当您在<input>元素中点击 return 时,浏览器会选择单击“麻烦”按钮。 But why?但为什么? What exactly is happening here?这里到底发生了什么? And where can I read it in the specs?我在哪里可以在规范中阅读它?

Both button elements have no type set.两个按钮元素都没有设置类型。 Type=submit "is the default if the attribute is not specified for buttons associated with a <form> " ( MDN Webdocs ) and troublesome is the next after the input field.如果没有为与<form>关联的按钮指定属性,则 Type=submit “是默认值”( MDN Webdocs )并且麻烦的是输入字段之后的下一个。 If you change its type to 'button', 'submit' will submit.如果您将其类型更改为“按钮”,则“提交”将提交。

From MDN as well: "Note that the submit event fires on the element itself, and not on any or inside it. However, the SubmitEvent which is sent to indicate the form's submit action has been triggered, includes a submitter property, which is the button that was invoked to trigger the submit request. If the submission was not triggered by a button of some kind, the value of submitter is null. (If there's no element type=submit and Enter is pressed inside the input, the form will submit with event.submitter = null)同样来自MDN :“请注意,提交事件在元素本身上触发,而不是在任何元素或元素内部触发。但是,发送以指示表单的提交操作已被触发的 SubmitEvent 包括一个提交者属性,它是触发提交请求的按钮。如果提交不是由某种按钮触发的,则提交者的值为空。(如果输入中没有元素 type=submit 并按下 Enter,则表单将提交与 event.submitter = null)

The logic seems to be "find the closest button to the focus that has type="submit", and simulate a click event on that button, else fall back to just submitting the form" (didn't find this explicitly somewhere) In your example 'troublesome' is of type 'submit' and closest to the input, and if there wasn't the 'prevent.default()', both events would be triggered.逻辑似乎是“找到最接近焦点的具有 type="submit" 的按钮,并在该按钮上模拟单击事件,否则回退到仅提交表单”(在某处没有明确找到)在您的示例 'troublesome' 的类型是 'submit' 并且最接近输入,如果没有 'prevent.default()',两个事件都会被触发。

 document.querySelector('form').onsubmit = ev => { ev.preventDefault(); document.querySelector('#result').innerText = ev.target.elements.text.value; }; document.querySelector('#troublesome').onclick = ev => { ev.preventDefault(); ev.target.innerText = Math.random(); };
 <form> <input type="text" name='text' value='something' /> <button type="button" id="troublesome">Trouble</button> <button>Submit</button> </form> <div id="result">not submitted</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM