Home >Web Front-end >JS Tutorial >Jquery responds to the enter key and directly submits the form operation code_jquery

Jquery responds to the enter key and directly submits the form operation code_jquery

WBOY
WBOYOriginal
2016-05-16 16:41:151334browse

The thing is like this, I made a login page, removed the Form, and directly used Jquery's Ajax to interact with the server, but in this case, the browser will not respond by default to the Enter key to submit the data. Simply let Jquery take over the response of the Enter key:

Copy code The code is as follows:

$("#loginbox input[type='submit']").click(function() {
//Ajax and server interactive verification
});
$('#loginbox').keydown(function(e){
If(e.keyCode == 13){
//Simulate clicking the login button and trigger the Click event above
​​​​ $("#loginbox input[type='submit']").click();
}
});

If you use keydown, IE6 may not work. Please read below for the solution

There are 3 keyboard events:

Keydown, keypress, and keyup respectively mean pressing down, holding down without raising, and raising the keyboard.

The correct code is:

Copy code The code is as follows:

$(document).keyup(function(event){
if(event.keyCode ==13){
$("#submit").trigger("click");
}
});

Recommended: keyup to prevent the laptop keyboard from being accidentally touched

1. Some documents have it written like this:

Copy code The code is as follows:

$(window).keydown(function(){
...
})

It is unsuccessful under IE6 under XP system.

2. There is also INPUT

Copy code The code is as follows:

$("input").keydown(function(){
...
})

In this case, keyboard events can only be monitored when the input gains focus.
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