views:

39

answers:

2

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?

+1  A: 
$("form").bind("keypress", function(e){
  if(e.keyCode == 13){
    $(this).submit();
  }
});
Marius
That'll bind to when someone presses the enter key while the submit link is in focus. I think he's talking about pressing the enter key inside an input, like most people expect forms to work.
Parrots
right, sorry. I fixed it now.
Marius
+1  A: 

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.

Parrots