If keyCode
is not caught, catch which
:
(如果keyCode
没有被捕获,赶上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
(编辑:错过了,最好使用keyup
而不是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.
(编辑2:与一些较新版本的Firefox一样,表单提交没有被阻止,将keypress事件添加到表单也更安全。)
Also it doesn't work (anymore?) by just binding the event to the form "name" but only to the form id.(它也不起作用(不再?)只是将事件绑定到表单“name”但仅限于表单id。)
Therefore I made this more obvious by changing the code example appropriately.(因此,我通过适当地更改代码示例使这一点变得更加明显。)
EDIT 3: Changed bind()
to on()
(编辑3:将bind()
更改为on()
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…