0

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

2 Answers 2

0

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>
0

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>
1
  • OP does say that they are able to send it on click, so they must have included it Apr 19, 2020 at 21:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.