jquery禁用表单提交输入 [英] jquery disable form submit on enter
问题描述
$('form')。在我的页面中有以下javascript,看起来没有工作。 (e.keyCode == 13){
e.preventDefault();
return false;
}
});
我想禁用在输入时提交表单,或者更好地调用我的ajax表单提交。任何一种解决方案都是可以接受的,但是我上面包含的代码并不会阻止表单提交。
如果 keyCode
未被捕获,请捕获其中
:
<$ ('keyup keypress',function(e){
var keyCode = e.keyCode || e.which;
if(keyCode === 13){
e.preventDefault();
return false;
}
});
编辑:错过了,最好使用 keyup $ c $编辑2:正如在Firefox的一些较新版本中,表单提交不会被阻止,将keypress事件添加到表单也更安全。此外,它只是通过将事件绑定到名称形式,但仅限于表单ID,它不能工作(再也不?)。因此,我通过适当地更改代码示例使这一点更加明显。编辑3:将
bind()
更改为 code> on()
I have the following javascript in my page which does not seem to be working.
$('form').bind("keypress", function(e) {
if (e.keyCode == 13) {
e.preventDefault();
return false;
}
});
I'd like to disable submitting the form on enter, or better yet, to call my ajax form submit. Either solution is acceptable but the code I'm including above does not prevent the form from submitting.
If keyCode
is not caught, catch which
:
$('#formid').on('keyup keypress', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
e.preventDefault();
return false;
}
});
EDIT: missed it, it's better to use keyup
instead of keypress
EDIT 2: As in some newer versions of Firefox the form submission is not prevented, it's safer to add the keypress event to the form as well. Also it doesn't work (anymore?) by just binding the event to the form "name" but only to the form id. Therefore I made this more obvious by changing the code example appropriately.
EDIT 3: Changed bind()
to on()
这篇关于jquery禁用表单提交输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!