Submitting a form on 'Enter' with jQuery?
$('.input').keypress(function (e) { if (e.which == 13) { $('form#login').submit(); return false; //<---- Add this line }});
Check out this stackoverflow answer:event.preventDefault() vs. return false
Essentially, "return false" is the same as calling e.preventDefault
and e.stopPropagation()
.
In addition to return false as Jason Cohen mentioned. You may have to also preventDefault
e.preventDefault();
Don't know if it will help, but you can try simulating a submit button click, instead of directly submitting the form. I have the following code in production, and it works fine:
$('.input').keypress(function(e) { if(e.which == 13) { jQuery(this).blur(); jQuery('#submit').focus().click(); } });
Note: jQuery('#submit').focus() makes the button animate when enter is pressed.