仅在_some字段上通过Enter键禁用表单提交 [英] Disable form submission via Enter key on only _some fields
问题描述
我想保留常规的当我按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.
寻找相关问题用于:
if (document.addEventListener) {
document.getElementById('strip').addEventListener('keypress',HandleKeyPress,false);
} else {
document.getElementById('strip').onkeypress = HandleKeyPress;
}
但是if (document.addEventListener) {
部分我不熟悉.
推荐答案
您可以在以下字段中捕获并取消输入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键禁用表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!