开发者

Pressing enter submits HTML form with one input, but not two

开发者 https://www.devze.com 2023-03-12 11:33 出处:网络
Can anyone explain this behavior? Pressing the Enter key in an HTML form\'s text box submits the form when the form contains a s开发者_StackOverflowingle text box, but not when the form contains two o

Can anyone explain this behavior? Pressing the Enter key in an HTML form's text box submits the form when the form contains a s开发者_StackOverflowingle text box, but not when the form contains two or more text boxes.

jsFiddle (one input): http://jsfiddle.net/gpPTa/

jsFiddle (two inputs): http://jsfiddle.net/fDbJt/


Unfortunately it's a default for the form to submit on enter with only one input.

You can either give each of them an javascript command that submits the form, or place a submit button with width: 0 and/or visibility: none. For example:

<form>
    <input style='width:0; visibility:hidden' type='submit'>
    <input>
    <input>
</form>


It seems that the browser assumes that since there is only one input, it is also the submit control. Focusing on it and pressing enter will submit the form, the same way as focusing on a submit button will behave.

When you add type="submit" to one of the <inputs>, you can use as many others as you like and the form will be submitted by pressing enter.

I don't have any references to back this up, but it seems logical to me.

0

精彩评论

暂无评论...
验证码 换一张
取 消