Bind the enter event code

  • 2020-03-30 02:59:32
  • OfStack

When doing the project, I encountered the situation of binding the "enter" key, and directly intercepted the scenario code. The code is as follows:
 
function sendLoginData(){ 
loginvalidateForm(); 

$(document).keydown(function(event){ 
if(event.keyCode == 13){ //Binding the enter
$('#login-submit').click(); / automatic / Trigger 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 message, improve the user experience degree
$('#loginInfoWord').show().text(' Processing in progress, please hold ...'); 
}, 
success:function(data){ 
var message = data.message; 
if(message == 'D'){ 
//Prompt message, improve the user experience degree
$('#loginInfoWord').show().text(' Login successful, jumping ...'); 
window.location.href = '{% url netPan.index.IndexHd%}'; 

}else if(message == 'C'){ 
$('#loginInfoWord').show().text(' Wrong username or password '); 
}else if(message == 'N'){ 
$('#loginInfoWord').show().text(' You haven't registered yet! '); 
}else if(message == 'H'){ 
$('#loginInfoWord').show().text(' You have not activated the account, please log in the email to activate the account! '); 
} 
}, 
error: function(xhr,textStatus,errorThrown){ 
$('#loginInfoWord').show().text(' An exception occurs :'+errorThrown); 
} 

}); 

}); 

/*  It doesn't matter what order you put it in  

$(document).keydown(function(event){ 
if(event.keyCode == 13){ //Binding the enter
$('#login-submit').click(); / automatic / Trigger login button  
} 
}); 

*/ 
} 

Related articles: