Home >Web Front-end >JS Tutorial >Problem with javascript Enter key triggering form submission_Form special effects

Problem with javascript Enter key triggering form submission_Form special effects

WBOY
WBOYOriginal
2016-05-16 18:29:15860browse

For example, for search behavior, you want to directly press the Enter key after entering the keywords to submit the form immediately. For some complex forms, you may want to avoid accidentally pressing the Enter key to trigger the form submission before completing the form filling.

To control these behaviors, there is no need to resort to JS. The browser has already done this for us. Here are a few rules:

If there is a button with type="submit" in the form , the Enter key takes effect.
If there is only one input with type="text" in the form, regardless of the button type, the Enter key will take effect.
If the button is not input, but button, and no type is added, the default is type=button in IE and type=submit in FX.
Other form elements such as textarea and select will not be affected, and the radio checkbox will not affect the triggering rules, but it will respond to the Enter key under FX but not under IE.
The input of type="image" has the same effect as type="submit". I don't know why such a type is designed. It is not recommended. It should be more appropriate to use CSS to add a background image.
In actual application, it is easy to make the form respond to the Enter key. Just make sure there is a button with type="submit" in the form. And what should I do when there is only one text box and I don’t want to respond to the Enter key? My method is a bit awkward, which is to write another meaningless text box and hide it. According to rule 3, when we use buttons, we should try to explicitly declare the type to ensure consistent browser performance.

made a demo (click to view) which lists some examples.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn