English 中文(简体)
JQuery:Keyup,如何防止箭头(向上和向下)和回车键的默认行为?
原标题:JQuery: Keyup, how do I prevent the default behavior of the arrow (up & down) and enter key?

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>

我有两个问题:

1) 当我按下向上箭头键时,插入符号不应该移动。

例如,在Chrome中,当我按下向上键时,它会将插入符号向左移动。但我只在Chrome中遇到这个问题。它在FF中运行良好。

2) 当我点击enter键时,我不希望提交表单。

顺便说一句,我想用按键而不是按下来实现这一点。

如果有任何想法,我将不胜感激。谢谢

最佳回答

我认为你做不到http://api.jquery.com/event.preventDefault/“rel=”noreferrer“>preventDefault()(您需要使用它来阻止浏览器对键执行默认操作)在keyup事件上-它在默认事件发生后被触发。请参阅此页了解更多信息。

如果可以,请考虑使用键向下

至于阻止表单提交,您可以绑定到submit事件返回false;当提交不是由提交按钮触发时(请参阅jQuery:如何获取表单提交时单击的按钮?了解如何确定)。

另一方面,您也可以绑定到表单的,当按下Enter时,以相同的方式取消提交(未测试代码):

$( form ).keypress(function(e){
    if(e.which === 13){
        return false;
    }
});
问题回答

您可以尝试返回false,或者让它调用一个函数并使用http://api.jquery.com/event.preventDefault/

例如:<code>case 40:函数(e){e.preventDefault;}

此外,<code>$(form).submit(function(){return false})会完全停止提交,但我不确定这就是你想要的吗?

为了防止用户按下回车键时提交表单,您需要绑定表单的提交功能,如果事件触发器是按下回车键,则返回false。

对于向上/向下按钮,请使用e.preventDefault()

最可靠的方法是e.preventDefault()e.stopPropagation()的组合返回false

以下是它可能的样子:

var override_keys = [13, 38, 40];

$( input ).keyup(function(e){
  var code = e.keyCode ? e.keyCode : e.which;

  if ($.inArray(code, override_keys)) {
    e.preventDefault();
    e.stopPropagation();
    return false;
  }
});

此外,为了防止使用回车键提交表单,您应该检查表单的submit事件:

$( #form ).submit(function(e){
  var code = e.keyCode ? e.keyCode : e.which;

  if ($.inArray(code, override_keys)) {
    return false;
  }
});

看起来你实际上可以在使用keyup()时停止ENTER的默认值。。。jQueryapi在这里自己写:http://api.jquery.com/keyup/

以下是您必须执行的操作:

$( textarea ).keyup(function(e) {
   // your code here
}).keydown(function(e){
   // your e.which for ENTER.
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.