Home >Web Front-end >JS Tutorial >jquery carriage return event implementation code_jquery

jquery carriage return event implementation code_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:03:061202browse

Example, usage of jquery keyboard event and enter key event.

// Keyboard events
1. keydown()
The keydown event will be triggered when the keyboard is pressed.

2. keyup()
The keyup event will be triggered when the key is pressed. Triggered when released, that is, the event after you press the keyboard and lift it up

3. keypress()
The keypress event will be triggered when the key is tapped. We can understand it as pressing and lifting the same key

Copy code The code is as follows:

//Enter key event
// Bind Define keyboard press event
$(document).keypress(function(e) {
// Enter key event
if(e.which == 13) {
jQuery(".confirmButton ").click();
} }
});
//Up and down key event
$(document).keydown(function(event){
//Judge when event.keyCode When it is 37 (i.e., the left side key), execute the function to_left();
//Judge when event.keyCode is 39 (i.e., the right side key), execute the function to_right();

if( event.keyCode == 37){
to_left();
}else if (event.keyCode == 39){
to_right();
}
});

Note: Due to the different keyboard press events of browsers, some events may not operate normally, so it is recommended to operate the keydown event!

Jquery monitors keys and presses the Enter key to trigger a method

Copy code The code is as follows:



Problem:
dragged a Login control, and then convert it into a template for custom development.

jquery carriage return event implementation code_jquery

In the following code, I captured the Enter button of the text bar. At this time, I want to trigger the LoginButton to submit the login information for verification, but I use $("[id$=LoginButton]").click() ;Only valid on Firefox, not IE, try $("[id$=LoginButton]").focus(); , this can be effective on IE, focus() will complete the focus on IE I clicked again and was puzzled. Haha~

Copy code The code is as follows:

$inp.keypress(function (event) {
var key = event.which;
if (key == 13) {
$("[id$=LoginButton]").click(); //Support firefox, IE martial arts school
/ /$('input:last').focus();
$("[id$=LoginButton]").focus(); //Supports IE, firefox is invalid. //The implementation of the above two sentences supports both IE and firefox

}
});


The code is as follows:

Global :

Copy code The code is as follows:
$(function(){
document. onkeydown = function(e){
var ev = document.all ? window.event : e;
if(ev.keyCode==13) {
$('#FormId).submit(); //Handle events
}
}
});

A certain control:

Copy code The code is as follows:
$('#id').keydown(function(e){
if(e.keyCode==13){
$('#FormId).submit(); //Handling events
}
});

if (window.event.keyCode==13) window.event.keyCode =0   //This will cancel the Enter key
If you want to simulate the Tab key, just write if (window.event.keyCode==13) window.event.keyCode=9, it will jump to another element superior.
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