jQuery热键 - 解除绑定? [英] jQuery Hotkeys - unbinding?

查看:191
本文介绍了jQuery热键 - 解除绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery对话框,其初始化热键如下:

 < script type =text / javascript> 
$(document).bind('keydown','<%=(i + 1)%>',function(evt){
// do stuff
});
< / script>

此循环通过1-9 ...



问题是,如果关闭对话框,然后重新打开该对话框。它保持重新绑定,所以当你在1上做一个keydown时,它会运行两次,三次,四次等等...它只是不断增长。



<我试图在对话框关闭时杀死键盘绑定。

  $(document).unbind('keydown','1') ; 
$(document).unbind('keydown','2');
$(document).unbind('keydown','3');
$(document).unbind('keydown','4');
$(document).unbind('keydown','5');
$(document).unbind('keydown','6');
$(document).unbind('keydown','7');
$(document).unbind('keydown','8');
$(document).unbind('keydown','9');

但这没有任何效果。任何有关如何处理这个问题的想法?



谢谢

解决方案

.unbind() 不支持 eventData 参数,这就是为什么你的解除绑定不起作用。



在我的头顶,你这里有两种不同的方法。如果这些是唯一的文档级别的键控绑定,则可以按如下方式完全解除绑定:

  $(document) .unbind( 'KEYDOWN'); // unbinds *所有* keydown处理文件

或者,您可以将keydown处理程序存储为非匿名函数,并在关闭对话框时保持引用返回解除绑定:

  function onkeydown(evt){
// do stuff
}

$(document).bind('keydown','<%=(i + 1)%>',onkeydown);

//稍后在对话框的关闭代码中:
$(document).unbind('keydown',onkeydown);但是,当同一功能被绑定多次时,我不是100%的正面效果,但是,b $ b


$ b。你可能更好的消除了 eventData ,并在事件处理程序中使用 event.which 来确定哪个键被按下(这将只需要处理程序绑定一次)。


I have a jQuery Dialog which initializes hotkeys as follows:

<script type="text/javascript">
  $(document).bind('keydown', '<%=(i+1)%>',function (evt) {
     // do stuff
  });
</script>

This loops through 1-9...

Problem is, if you close the dialog and then reopen the dialog. It keeps re-binding, so when you do a keydown on '1', it then runs twices, three times, four times, etc... it just keeps growing.

I tried killing the keybindings on dialog close with

$(document).unbind('keydown', '1');
$(document).unbind('keydown', '2');
$(document).unbind('keydown', '3');
$(document).unbind('keydown', '4');
$(document).unbind('keydown', '5'); 
$(document).unbind('keydown', '6');
$(document).unbind('keydown', '7');
$(document).unbind('keydown', '8');
$(document).unbind('keydown', '9');

But that had no effect. Any ideas on how to handle this?

Thanks

解决方案

Note that .unbind() doesn't support the eventData argument, which is why your unbinds aren't working.

Off the top of my head, you have two different approaches here. If these are the only document-level keydown bindings, you can to a "full" unbind as follows:

$(document).unbind('keydown'); // unbinds *all* keydown handers on the document

Alternatively, you can store your keydown handler as a non-anonymous function and keep a reference around to pass back to unbind when closing the dialog:

function onkeydown(evt) {
   // do stuff
}

$(document).bind('keydown', '<%=(i+1)%>', onkeydown);

// later, in the dialog's "shutdown" code:
$(document).unbind('keydown', onkeydown);

I'm not 100% positive how this works when the same function is bound multiple times, however. You're probably better off eliminating the eventData and using event.which inside your event handler to determine which key was pressed (which would then only require the handler be bound once).

这篇关于jQuery热键 - 解除绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆