简体   繁体   中英

How to execute a listener bound to an event and exclude the remaining listeners bound to different events from execution?

I have the following jQuery code:

$input.on('keyup', keyUpListener);

$input.on('input', inputListener);

// IE <= 8 fallback for input event.
$input[0].onpropertychange = function() {
    if (window.event.propertyName === "value") {                
        inputListener(window.event);
    }
};

$input is a jQuery input[type='text'] .

Right now keyUpListener and inputListener are both executed when I type into the input or when I copy and paste something ( onpropertychange is not fired because it is an IE only event).

But how can I tell JS to not fire inputListener if keyUpListener is executing and vice-versa?

Attach the specific event to the textbox?

$("#input").on('input', function () {
}

For instance the above - this way you can have multiple listeners not executing on the same input

EDIT

You are able to bind the paste function to the textbox also..

$("#input").bind('paste', function() {
var pasted = true;
}); 

Thus you can then have the below IF statement:

if (pasted) {
$input.on('input', inputListener);
} else {
$input.on('keyup', keyUpListener);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM