Based on 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.
My expectation is when a form has multiple input elements (and no submit button) then form should not get submitted implicitly when user hits the enter button.
I have a form with a button (not submit button) and multiple input elements.
<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>
with above HTML, when cursor is inside the First Name text box and user press enter then the form get submitted. I am not sure why form is getting submitted when there are multiple input elements inside the Form.
I am using Chrome
My observation: If I change hidden
input element as below then form does NOT get submitted implicitly.
From
<input id="itemid" type="hidden" />
To
<input id="itemid" hidden />
CodePudding user response:
So as I said in my comment, you forgot to add the part of the citation that states:
"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"
So inputs like radio buttons, checkboxes, and hidden types aren't on that list, so they don't apply. When you have a second input like <input id="itemid" type="hidden" />
you explicitly set the type to hidden, so it won't block submission. Removing the type so you have <input id="itemid" />
does stop implicit submission because the default type of an input is text
, which is on that list, so it stops implicit submission. Similarly, when you switch the input to <input id="itemid" hidden />
the type is still text
, so it also stops the implicit submission.