Home > Web Front-end > JS Tutorial > Solution to the problem that forms in web pages are automatically submitted when pressing Enter_Basic knowledge

Solution to the problem that forms in web pages are automatically submitted when pressing Enter_Basic knowledge

WBOY
Release: 2016-05-16 16:32:10
Original
1637 people have browsed it

1. When there is only one in the form, pressing the Enter key will automatically submit the form.

Copy code The code is as follows:




Add one more

Copy code The code is as follows:


Pressing Enter will not automatically submit, but it is awkward to display an unintelligible input box on the page. I then found two solutions from the Internet:
(1) Add a

Copy code The code is as follows:


The input box will not be displayed, and it will not be submitted after pressing Enter:

Copy code The code is as follows:





(2) Add an onkeydown event, and it will not be displayed after pressing Enter:

Copy code The code is as follows:




If you want to add a carriage return event, you can add a judgment submission form in the onkeydown event:

Copy code The code is as follows:





Sometimes we want to press the Enter key in the text box (input element) to submit the form (form), but sometimes we don't want this. 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 of type="submit" in the form, the Enter key will take effect.

If there is only one input of 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 is more appropriate to use CSS to add a background image.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template