简体   繁体   中英

Submitting forms on enter/return when using anchors as submit-input

I'm using anchors to submit forms:

$("a[title=submit]").click( function(){
      $(this).parents("form").submit();
});

It works very well, however I still want the user to be able to submit the form when pressing enter or return.

Anyone know how to do this?

$("form").bind("keypress", function(e){
  if(e.keyCode == 13){
    $(this).submit();
  }
});

You should be able to do that by default. If you have an onsubmit for the form make sure it returns true, otherwise returning false will prevent the form from submitting normally.

Unless your submit function is doing something funky it shouldn't block the user from submitting by pressing enter inside a field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM