Вопрос:

Multiple overriding 'keyup' events

javascript jquery

274 просмотра

2 ответа

108 Репутация автора

In my application, I am listening to the keyups on a text input like this:

$('input[type="text"]').keyup(function(){

This works fine, however I also want to listen for the user pressing the UP or DOWN key anywhere in the rest of the document - so I tried adding the following (hoping jquery would use the input one inside the text box and the global one for anything else):

$(document).keyup(function(e) {

It has not done this, and chooses the global keyup for everything now. Is there any way I can get Jquery to do what I need here? I feel like it should be choosing the 'most specific' key up in each case, but it seems not...

Автор: Kevin Pione Источник Размещён: 22.08.2016 09:43

Ответы (2)


1 плюс

478169 Репутация автора

It should be running both handlers, starting with the one for the more specific element. But if it's not, you can change the second handler so it doesn't overlap.

$(document).on("keyup", ":not(:text)", function(e) {
    ...
});

:text is a shorthand selector for input[type=text], and :not() excludes them in this binding.

Автор: Barmar Размещён: 22.08.2016 09:58

2 плюса

36 Репутация автора

Решение

you can use this way for listening to the user when pressing the UP key anywhere from entire document elements with exception for input[type='text']

<script>
        $(document).keyup(function(e) {
           if($(e.target).is('[type="text"]')){
              return; <!-- you can handle keyup of input type text in this block -->
           } 
           else {
                <!-- handle your keyup of ducument code here -->
           }
        });
</script> 
Автор: Wadhah Sky Размещён: 22.08.2016 10:55
Вопросы из категории :
32x32