JavaScript press Enter the appropriate button to submit event _javascript tips

Source: Internet
Author: User
1, using the Submit button submission, in form forms of the OnSubmit event for form verification:
Copy Code code as follows:

<script type= "Text/javascript" >

function Onsub () {

Form validation Code

}

</script>

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

2, use button or picture of the OnClick event call form validation code:
Copy Code code as follows:

<input type= "button" name= "button" id= "button" onclick= "Javascript:onsub ();"/>



However, when submitting a form in the second way, you cannot press ENTER to submit the form when the form is filled out. This gives the customer the

The feeling is different from submitting the form directly using the submit button; In order to implement this feature, just add the following JavaScript code to your page
Copy Code code 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;
}
}

Start onkeydown Event Call Butonclick () function in one of the most entries in your form; For example, the last entry in the login program is a password,

<input type= "Password" name= "userpwd" onkeydown= "Javascript:butonclick" (); "/>

This way, when you enter the password, you can press ENTER to implement form verification and login (if the username and password are correct). This is a personal summary, shared out

Everybody share!

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.