Home >Web Front-end >JS Tutorial >javascript press enter key corresponding button to submit event_javascript skills

javascript press enter key corresponding button to submit event_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:42:331137browse

1. Use the submit button to submit and perform form verification on the onsubmit event in the form form:

Copy the code The code is as follows:





2. Use button Or the onclick event of the image calls the form verification code:
Copy code The code is as follows:

< input type="button" name="button" id="button" onclick="javascript:onSub();" />



However, when using the second method to submit the form, you cannot submit the form by pressing Enter after filling in the form. This gives customers a

feeling that is different from directly using the submit button to submit the form; in order to implement this function, just add the following javascript code to your page
Copy code The 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;
}
}

Trigger the onkeydown event in the last input item of your form and call the butOnClick() function; for example, if the last item in the login program is a password, then



In this way, after you enter the password, press the Enter key to achieve form verification and login operations (if the username and password are correct) . This is a personal summary, please share it

Share it with everyone!
Statement:
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