input type =text, press ENTER to submit automatically

Source: Internet
Author: User

1, when the form form has only one <input type= "text" name= ' name '/>, press the ENTER key will automatically submit the form

<IDActionmethod= ' post '>  <typename/> </  Form>
View Code

Add a <input type= "text"/> Press Enter will not be automatically submitted, but the page shows a unintelligible input box very awkward, after searching from the Internet two solutions:

1; Add a <input type= ' text ' style= ' display:none '/> Do not display the input box, and then enter the following will not be submitted:

<formID= ' Form1 'Action= ' a1.jsp 'Method= ' Post '><inputtype= ' text 'name= ' name '/><inputstyle= ' Display:none '/></form>
View Code

2. Add a onkeydown event and then enter it again without displaying:

 <  form  id  = ' Form1 '  action  = ' a1.jsp '  method  = ' Post '  >  <  input  type  = ' text '  name  = ' name '  onkeydown  = ' if (event.keycode==13)  return false; '  />  </ form  >  
View Code

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

<formID= ' Form1 'Action= ' a1.jsp 'Method= ' Post '><inputstyle= ' Display:none '/><inputtype= ' text 'name= ' name 'onkeydown= ' if (event.keycode==13) {gosubmit ();} '/></form>
View Code

Sometimes we want the enter key to be in the text box (input Element) to submit the form (form), but sometimes it's not desirable. such as search behavior, I hope to enter the keyword immediately after the key to submit the form, and some complex forms, you may want to avoid the enter error operation when the completion of the form to fill out the form to trigger the submission of forms.

To control these behaviors, do not need to use JS, the browser has helped us do these processing, here summarizes a few rules:

If there is a type= "submit" button in the form, the ENTER key will take effect.
If there is only one type= "text" Input in the form, the enter takes effect regardless of the type of the button.
If the button is not with input, but with a button, and no Type,ie is added, the default is type=button,fx default to Type=submit.
Other form elements such as textarea, select Do not affect the Radio checkbox does not affect the triggering rules, but itself in the FX will respond to the enter, under IE do not respond.
Type= "image" input, the effect is equivalent to type= "submit", do not know why the design of such a type, is not recommended, should be used to add a CSS background map appropriate.

input type =text, press ENTER to submit automatically

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.