Ways to prevent submitting forms by pressing ENTER

Source: Internet
Author: User
Tags commit

There are two possible scenarios in which autocommit occurs:

One is to write the JavaScript code, when the user click the Enter key, through the JS event listening mechanism to contact the publication of the submission.

The second is to take advantage of the default behavior of the browser (ie is found at least). When a browser parses a Web page, there are many default behaviors, such as if a page has a form and a Submit button, the focus automatically falls on the Submit button when the page is opened. Also, if you have only one single line of text input field (text) in a form, the browser automatically submits the form when you press ENTER in the input field.

We are generally aware of the first situation and easy to understand, but for the second browser's default behavior, there are fewer people who might know this, and I'll look at the default behavior of the browser (at least IE) on the form submission.

If the form contains a single line of text input field, the form is automatically submitted, regardless of how many other types of form components are included, when you click Enter in the input field.

For example, the following code:

1 2 3 4 <form action= "" method= "post" > <input type= "text" name= "Sdfsdf"/> <input type= "checkbox" >sdfsdf < Input type= "hidden" name= "AA"/></form>

If the form contains two or more single-line text input fields, pressing the ENTER key does not automatically commit, regardless of whether there are other types of form components, such as:

1 2 3 4 5 <form action= "" method= "post" <input type= "text" name= "SDFSDF"/<input type= "text" name= "SDDF"/</form

The method is very simple, we have in the example mentioned above, just add a text input box on it, you may say, in order not to automatically commit to add a useless input box, and contains two input boxes end users will accept it? Can be solved, you can hide the newly added input box through the style, for example:

1 2 3 4 5 <form action= "" method= "post" <input type= "text" name= "Notautosubmit" style= "Display:none"/<input type= "text "Name=" username "/</form

There is also a way to bind the button button ENTER to trigger an event:
document.onkeypress = function () {
if (Event.keycode = =) {search (); returnfalse;}} Where the search method is the onclick event: <form name= "Searchfrom"

Final Solution:

1 2 3 4 5 6 7 8 9, <script language= "JavaScript" > Function Definesubmit (btn) {if ("submit1" = = Btn.value) {document.testform.action= "firstaction";} else {document.testform.action= "secondaction";}   Document.testForm.submit (); } </script> <form name= "Testform" method= "POST" > Username:<input type= "text" name= "username"/> Password:<input type= "password" name= "password"/> <input type= "button" Name= "Submitname" onclick= " Definesubmit (This) "value=" Submit1 "/> <input type=" button "Name=" Submitname "onclick=" Definesubmit "(This)" Value= "Submit2"/> </form>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.