问题描述
我想保留传统的按 Enter 时提交表单"行为,因为用户很熟悉.但是反射性地,他们经常在完成文本输入框时按 Enter 键 - 但在他们实际完成完整表单之前.
I want to retain the conventional 'form submits when i press Enter' behavior because users are familiar with. But by reflex, they often hit enter when they finish with a text input box - but before they are actually done with the complete form.
我想仅在关注特定类别的输入时劫持 Enter 键.
I'd like to hijack the Enter key only when then focus is on a certain class of input.
查看 相关问题 这看起来像我在看的对于:
Looking Related Questions this looks like what I'm looking for:
if (document.addEventListener) {
document.getElementById('strip').addEventListener('keypress',HandleKeyPress,false);
} else {
document.getElementById('strip').onkeypress = HandleKeyPress;
}
但是 if (document.addEventListener) {
部分我不熟悉.
but the if (document.addEventListener) {
part is unfamiliar to me.
推荐答案
您可以像这样在这些字段上捕获和取消输入 keypress
:
You can capture and cancel the enter keypress
on those fields like this:
$('.noEnterSubmit').keypress(function(e){
if ( e.which == 13 ) return false;
//or...
if ( e.which == 13 ) e.preventDefault();
});
然后在你的输入上给他们一个 class="noEnterSubmit"
:)
Then on your inputs just give them a class="noEnterSubmit"
:)
展望未来,以防其他人稍后发现这一点,在 jQuery 1.4.3(尚未发布)中,您可以将其缩短为:
Looking ahead in case others find this later, in jQuery 1.4.3 (not out yet) you can shorten it to this:
$('.noEnterSubmit').bind('keypress', false);
这篇关于仅在 _some 字段上通过 Enter 键禁用表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!