I got a login form which has a button that is click to call ajax request, the request is trigger only if i mouse clicked on the button instead of press the enter key,
<form id="signin_form">
Email: <input type="text" id="email" name="email">
Password: <input type="password" id="password" name="password">
<input type="button" id="btn_signin" value="Sign In">
</form>
I found a working sample from here (thanks to Stackoverflow) by how to make it on keypress, below is almost the same reaction, can below scripts merge together in order to make the scripts clean?
$("#password").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
var parameters = $('#signin_form').serialize();
$.ajax({
url: baseurl + 'loginRequest',
type: 'POST',
data: parameters,
dataType: 'json',
success: function(output_str){
if(output_str == "success"){
window.location.replace(window.location);
}else{
$('#result').html(output_str);
}
}
});
}
});
// login request
$('#btn_signin').click(function(){
var parameters = $('#signin_form').serialize();
$.ajax({
url: baseurl + '/login',
type: 'POST',
data: parameters,
dataType: 'json',
success: function(output_str){
if(output_str == "success"){
window.location.replace(window.location);
}else{
$('#result').html(output_str);
}
}
});
});
Thanks.
1 Answer