JQuery:Keyup,如何防止箭头(向上和向下)和回车键的默认行为?
JavaScript (JQuery)
$('input').keyup(function(e)
{
var code = e.keyCode ? e.keyCode : e.which;
switch(code)
{
case 38:
break;
case 40:
break;
case 13:
break;
default:
return;
}
});
HTML
<form method="post" action="/">
<input type="text" name="text" />
<button type="submit">Submit</button>
</form>
我有 2 个问题:
1) 当我按下向上箭头键时,插入符号不应该移动。
例如,在 Chrome 中,当我按下向上键时,它会将插入符号移至左侧。但我只在 Chrome 中遇到这个问题。在FF中运行良好。
2)当我按下回车键时,我不想提交表单。
顺便说一句,我想让它与 keyup 一起使用,而不是与 keypress 一起使用。
我将不胜感激任何想法。谢谢。
JavaScript (JQuery)
$('input').keyup(function(e)
{
var code = e.keyCode ? e.keyCode : e.which;
switch(code)
{
case 38:
break;
case 40:
break;
case 13:
break;
default:
return;
}
});
HTML
<form method="post" action="/">
<input type="text" name="text" />
<button type="submit">Submit</button>
</form>
I have 2 problems:
1) The caret shouldn't move when I hit the up arrow key.
For example, in Chrome when I hit the up-key it moves the caret to the left. But I only have this problem in Chrome. It works fine in FF.
2) When I hit the enter key, I don't want the form to be submitted.
BTW, I want to get this to work with keyup and not keypress.
I'd appreciate any ideas. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你不能
preventDefault()
(这是您需要使用什么来阻止浏览器对keyup
事件执行某个键的默认操作 - 它在默认事件发生后被触发。请参阅此页面了解更多信息。如果可以的话,请考虑使用
keydown
来代替。至于停止表单提交,您可以绑定到
submit
事件并return false;
当提交不是由提交按钮触发时(请参阅jQuery:如何获取在提交表单时单击了哪个按钮? 了解如何确定这一点)。另一方面,您还可以绑定到表单的
keypress
,当按下 Enter 时,以相同的方式取消提交(未经测试的代码):I don't think you can
preventDefault()
(which is what you'd need to use to stop the browser from performing the default action for a key) on thekeyup
event - it is fired after the default event has already occurred. See this page for more.If you can, consider using the
keydown
instead.As for stopping the form from submitting, you could bind to the
submit
event andreturn false;
when the submit was not triggered by the submit button (see jQuery: how to get which button was clicked upon form submission? for how to determine this).On the other hand, you could also bind to
keypress
for the form and when Enter is pressed, cancel submission the same way (untested code):您可以尝试 return false 或使其调用函数并使用 http://api.jquery,而不是中断。 com/event.preventDefault/
例如:
case 40: function(e) {e.preventDefault;}
另外,
$('form').submit(function() {return false});
会完全停止提交,但我不确定这就是你想要的?Instead of break you could try return false or make it call a function and use http://api.jquery.com/event.preventDefault/
For example:
case 40: function(e) {e.preventDefault;}
Also,
$('form').submit(function() {return false});
would stop submissions altogether, but I'm not sure that's what you're after?为了防止用户按下 Enter 时提交表单,您需要绑定表单的提交函数,如果事件触发器是按下 Enter 按钮,则返回 false。
对于向上/向下按钮按下,请使用
e.preventDefault();
In order to prevent the form being submitted when the user presses enter you need to bind the submit function of your form and return false if the event trigger was the enter button being pressed.
for the Up/Down button press, use
e.preventDefault();
最可靠的方法是
e.preventDefault()
、e.stopPropagation()
和return false
的组合;它可能如下所示:
另外,为了防止使用 Enter 键提交表单,您应该检查表单的
submit
事件:The most reliable method is a combination of
e.preventDefault()
,e.stopPropagation()
andreturn false
;Here's what it might look like:
Also, to prevent the form from being submitted with the enter key you should check the form's
submit
event:看起来你实际上可以在使用 keyup() 时停止默认的“ENTER”; ... jQuery api 在这里写了它自己: http://api.jquery.com/keyup/
您必须这样做:
It seems like that you actually CAN stop the default for 'ENTER' while using keyup(); ... jQuery api writes about it themselves here: http://api.jquery.com/keyup/
Here's how you have to do: