$(document).bind('ke...">

JQuery hotkeys - decoupling?

I have a jQuery Dialog that initializes hotkeys as follows:

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

This happens after 1-9 ...

The problem is that if you close the dialog, then open the dialog again. It continues to rebind, so when you do a keydown to "1", it then runs twices three, four times, etc ... it just keeps growing.

I tried to kill the key bindings in the dialog box next to

$(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 it had no effect. Any ideas on how to handle this?

thank

+3
source share
3 answers

Note that it .unbind()does not support the argument eventData, so your unties do not work.

. , "" :

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

keydown , :

function onkeydown(evt) {
   // do stuff
}

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

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

100% , , . , eventData event.which , , ( ).

+3

, keydown ()

.g "keydown.g". , , .

$(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

+1

you can use

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

This method binds events once when loading a document.

0
source

Source: https://habr.com/ru/post/1790057/


All Articles