HTML form隱式提交
有時在HTML頁面form的input里按了回車鍵會提交該表單,並且form的submit按鈕的click事件也會被觸發.這是什麼原理呢?是因為form的隱式提交(Implicit submission)機制
在https://www.w3.org/TR/html52/sec-forms.html#implicit-submission中是這麼解釋的
A form element』s default button is the first Submit Button in tree order whose form owner is that form element.
form的第一個submit按鈕作為default button
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.
瀏覽器隱式提交時要觸發default button的click事件.在某些平台上,text field獲得焦點時,按回車鍵會觸發隱式提交
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.
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
form里有超過一個的input時,必須阻止隱式提交
在早期的HTML規範中,還有如下描述
When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.
如果form里只有一個text input,那麼那個input按回車要觸發form提交
地址是https://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.2
推薦閱讀:
※一個網頁的漂洋過海之旅:起航
※初識markdown
※學習web開發如何合理分配時間?
※HTML5 的普及需要具備哪些因素?
※解決 Jenkins 中無法展示 HTML 樣式的問題
TAG:HTML |