Jquery responds to the enter key by submitting the form action code directly

  • 2020-03-30 03:35:47
  • OfStack

The thing is, you make a landing page, you take the Form, you interact with the server directly with Jquery's Ajax, but then the browser doesn't respond to the return key by default to submit the data. Let Jquery take over the return key as well:


$("#loginbox input[type='submit']").click(function() {
    //Ajax interacts with the server to validate
});
$('#loginbox').keydown(function(e){
    if(e.keyCode == 13){
        //Simulate clicking the login button to trigger the Click event above
        $("#loginbox input[type='submit']").click();
    }
});

If you use keydown, IE6 may not work, please read on to see the solution

Keyboard events have 3:

Keydown, keypress, keyup, respectively, is to press, press not up, up the keyboard.

The correct code is:


$(document).keyup(function(event){
  if(event.keyCode ==13){
    $("#submit").trigger("click");
  }
});

Recommend: keyup, prevent notebook keyboard accidentally touched

1. Some documents are written like this:


$(window).keydown ( function(){
...
} )

XP system IE6 is not successful.

2. There is also INPUT


$("input").keydown ( function(){
...
} )

In this case, the keyboard event cannot be listened for until the input gets focus.


Related articles: