Home > Web Front-end > JS Tutorial > Bind enter event code_jquery

Bind enter event code_jquery

WBOY
Release: 2016-05-16 16:47:57
Original
1404 people have browsed it

When working on a project, I encountered the situation of binding the "enter" key, so I directly intercepted the scenario code. The code is as follows:

Copy code The code is as follows:

function sendLoginData(){
loginvalidateForm ();

$(document).keydown(function(event){
if(event.keyCode == 13){ //Bind Enter
$('#login-submit ').click(); /Automatically/trigger the login button
}
});

$('#login-submit').click(function(){
if( $('#login-form').valid()==false){
return false;
}

var username = $('#id_username').val();
var password = $('#id_password').val();
$.ajax({
type:"post",
dataType:"json",
contentType:"application /x-www-form-urlencoded;charset=UTF-8",
url:"{% url netPan.User.views.LoginHd%}",
data:{
username: username,
password: password
},
beforeSend: function(){
//Prompt information to improve user experience
$('#loginInfoWord').show().text('is Processing, please wait...');
},
success:function(data){
var message = data.message;
if(message == 'D'){
//Prompt information to improve user experience
$('#loginInfoWord').show().text('Login successful, jumping...');
window.location.href = '{% url netPan.index.IndexHd%}';

}else if(message == 'C'){
$('#loginInfoWord').show().text('User Incorrect name or password');
}else if(message == 'N'){
$('#loginInfoWord').show().text('You have not registered yet! ');
}else if(message == 'H'){
$('#loginInfoWord').show().text('You have not activated your account yet, please log in to your email to activate your account!' );
}
},
error: function(xhr,textStatus,errorThrown){
$('#loginInfoWord').show().text('Exception occurred:' errorThrown);
}

});

});

/* It can be placed above or below, the order has nothing to do with it

$(document) .keydown(function(event){
if(event.keyCode == 13){ //Bind Enter
$('#login-submit').click(); /Automatically/trigger the login button
}
});

*/
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template