jQuery Hotkeys - unbinding?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 18:35:34

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).

I resolved a similar issue using a namespace for the keydown event in conjunction with one()

note the .g in "keydown.g". That puts it in a separate scope which I can later unbind without unbinding every keydown in the document.

$(document).one("keydown.g", function(e) {
 // tab key
 if (e.which == "9") {
  e.preventDefault();
  // do something like focus on a field
  $("#target").focus();
  // once you've moved the focus, you can unbind and go back to tabbing normally
  $(document).unbind("keydown.g");
 } 
});

<3 Jquery

You can use

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

This method binds events one time when document loaded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!