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.

I'd like to hijack the Enter key only when then focus is on a certain class of input.

Related Questions这看起来像我要找的:

if (document.addEventListener) {
    document.getElementById('strip').addEventListener('keypress',HandleKeyPress,false);
} else {
    document.getElementById('strip').onkeypress = HandleKeyPress;
}

but the if (document.addEventListener) { part is unfamiliar to me.

推荐答案

您可以在以下字段中捕获并取消输入keypress:

$('.noEnterSubmit').keypress(function(e){
    if ( e.which == 13 ) return false;
    //or...
    if ( e.which == 13 ) e.preventDefault();
});

然后根据你的输入给他们打class="noEnterSubmit"分:)

展望future ,如果其他人稍后发现这一点,在jQuery 1.4.3(尚未发布)中,您可以将其缩短为:

$('.noEnterSubmit').bind('keypress', false);

Jquery相关问答推荐

使用 shell 脚本判断 json 数组响应是否具有特定用户名和状态的 jq 命令

jQuery .first() 方法返回第一个元素的集合

5 秒后关闭 jQuery 弹出模式

我正在try 使用 Jquery 打开与帖子相关的特定 comments

在正则表达式字符括号中包含连字符?

如何滚动到AngularJS中的页面顶部?

如何在 jquery 中从这个日期减go 一周?

这些 jQuery 就绪函数之间有什么区别?

用jquery查找div底部的位置

如何在jQuery中 Select 具有特定ID的所有元素?

在 Javascript 中,字典理解或 Object `map`

Ajax 更新后在 jQuery 中重新绑定事件(更新面板)

jQuery '如果 .change() 或 .keyup()'

你如何在Javascript中缓存图像

如何使用 jQuery 停止默认链接点击行为

使用 JavaScript 获取用户代理

延迟jquery悬停事件?

如何使用 jQuery 格式化电话号码

为什么要定义一个匿名函数并将 jQuery 作为参数传递给它?

如何使用 ID 变量在 jQuery 中 Select 元素?