I have one form and I posted POST with Ajax and everything is beautiful. But when I press enter, the url is get and ajax is not working. I want Ajax to work when you press enter ,My codes:
function ajax2(){
$.ajax({
type:"POST",
url:"ajax/alock.php",
data:$("#formlock").serialize(),
success:function(e){
$("#result").html(e);
}
});
}
$("#btnlock").click(function(){
ajax2();
});
/*I want Ajax to work when you press enter */
$("#formlock input").keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
ajax2();
}
});
<form role="form" class="form-inline" id="formlock">
<div class="form-group col-lg-12">
<input type="password" placeholder="Şifre" name="pass" class="form-control lock-input">
<button class="btn btn-lock" type="button" id="btnlock">
<i class="fa fa-arrow-right"></i>
</button>
</div>
</form>
2
Answers
You need to include ajax library to the file
Test on jsFiddle with jQuery 3.4.1,
Your code work great … Check your jQuery include
or compatibility : https://www.quirksmode.org/dom/events/keys.html
https://jsfiddle.net/c1vruLhp/