Javascript press the Enter key button to submit the event

Source: Internet
Author: User

1. Use the submit button to submit and perform form verification on the onsubmit event in the Form: CopyCode The Code is as follows: <SCRIPT type = "text/JavaScript">

Function onsub (){

// Form Verification Code

}

</SCRIPT>

<Form action = "" method = "" onsubmit = "javascript: onsub ();">

2. Use the onclick event of a button or image to call the Form Verification Code:Copy codeThe Code is as follows: <input type = "button" name = "button" id = "button" onclick = "javascript: onsub ();"/>

However, when you use the second method to submit a form, you cannot press enter to submit the form. This gives the customer

The experience is different from using the submit button to submit a form. To implement this function, you only need to add the following JavaScript code to your page.Copy codeThe Code is as follows: function butonclick (){
If (event. keycode = 13 ){
VaR button = Document. getelementbyid ("bsubmit"); // bsubmit is the ID of the botton button
Button. Click ();
Return false;
}
}

You can call the butonclick () function for the onkeydown event in the most input field of your form. For example, log onProgramIs the password

<Input type = "password" name = "userpwd" onkeydown = "javascript: butonclick ();"/>

In this way, after entering the password, you can press the Enter key to verify the form and log on (if the user name and password are correct ). This is my personal summary and shared.

Let's share it!

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.