skip to Main Content

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:

enter image description here

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


  1. You need to include ajax library to the file

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    
    Login or Signup to reply.
  2. 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/

    <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>
    
    
        <script>
            function launch() {
                alert('you press enter');
            }
    
            $("#formlock input").keypress(function(event){
                var keycode = (event.keyCode ? event.keyCode : event.which);
                if(keycode == '13'){
                   launch();
                }
            });
        </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search