5
<form method="POST" action="">
    <input type="text" />
    <input type="text" />
</form>

(JSFiddle)

Press enter on the input. Then delete one of the inputs and press it again. When we have two inputs inside of the form, the form is not submitted (we need a button then). Why is that?

good_evening
  • 21,085
  • 65
  • 193
  • 298

3 Answers3

7

The default behavior of HTML forms with a single input is to submit on enter. As soon as you add a second input, pressing enter will no longer submit the form. The presense or lack of names has nothing to do with it.

Brandon Gano
  • 6,430
  • 1
  • 25
  • 25
  • That seems to be so, but I'm having trouble finding any specs for this behaviour. – Damien_The_Unbeliever Dec 11 '11 at 06:08
  • @Damien_The_Unbeliever I cam across this reference from the JQuery site Depending on the browser, the Enter key may only cause a form submission if the form has exactly one text field, or only when there is a submit button present. – Steve Robillard Dec 11 '11 at 06:10
  • I'd also like to see a reference for this behavior, if one exists. Like @Damien_The_Unbeliever I'm having trouble finding it anywhere in the HTML spec. – Andrew Marshall Dec 11 '11 at 06:18
  • 4
    Finally found a link to an [old spec](http://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.2), from a [related question](http://stackoverflow.com/questions/1370021/enter-key-on-a-form-with-a-single-input-field-will-automatically-submit-with-ge): "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." – Damien_The_Unbeliever Dec 11 '11 at 06:22
  • Good find there @Damien_The_Unbeliever. Wouldn't have imagined it'd be buried all the way in the HTML 2.0 spec! – Andrew Marshall Dec 11 '11 at 06:24
3

The original idea was that very simple forms, typically consisting just of a text input box e.g. for searching, should be easy and quick: the user just types a search word and hits Enter. Controls of other types, such as checkboxes, do not affect this. Virtually all browsers implemented this idea.

But, according to the old idea, if there are several text input boxes, there are too big risks of premature form submission: the user hits Enter, expecting to get to the next field, or maybe just by accident.

Yet, IE introduced (in IE 4) the feature that makes Enter in a text input box submit the form, even if the form contains several such elements. Later, other browsers followed suit, and nowadays this (mis?)behavior appears to be “standard.” This feature has been claimed to be an usability improvement, and in some special cases, it really is. More often, it is a risk, and therefore pages often try to use client-side scripting to prevent it.

However, at least on most modern browsers, this only happens when the form has a submit button. But if you omit the submit button, users will get puzzled, as most forms have a submit button or buttons.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
0

Unless you put a button or input[type=submit] somewhere in the form. The button can be wrapped inside visibility:hidden css if you don't want it to show up.

Fred
  • 11
  • 1