Submitting a form on 'Enter' with jQuery?
I have a bog-standard login form - an email text field, a password field and a submit button on an AIR project that's using HTML/jQuery. When I hit Enter on the form, the entire form's contents vanish, but the form isn't submitted. Does anyone know if this is a Webkit issue (Adobe AIR uses Webkit for HTML), or if I've bunged things up?
I tried:
$('.input').keypress(function (e) {
if (e.which == 13) {
$('form#login').submit();
}
});
But that neither stopped the clearing behavior, or submitted the form. There's no action associated with the form - could that be the issue? Can I put a javascript function in the action?
$('.input').keypress(function (e) {
if (e.which == 13) {
$('form#login').submit();
return false; //<---- Add this line
}
});
NOTE: You accepted bendewey's answer, but it is incorrect with its description of e.preventDefault(). 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.
链接地址: http://www.djcxy.com/p/19526.html上一篇: 禁用href标记
下一篇: 使用jQuery在'输入'提交表单?