简体   繁体   中英

Cancel key up event codes in key down event codes

This is my codes

    $(textBoxElement).keydown(function(e)
 {
  if(e.keyCode==40)
   {
    //some codes
    //cancel key up event 
   }
 }
)
 $(textBoxElement).keyup(function()
  {
    //some codes
  }

I want user when pressing down arrow key keyup event codes don't run

return false; and e.stopPropagation; dont work

The event will still be triggered, there is no way to prevent the event from firing - but what you can change, is how you handle that event. Take the following code for example:

var disableKeyUp = false;
$(textBoxElement).keydown(function(e) {
  disableKeyUp = true
})
$(textBoxElement).keyup(function(e) {
  if (!disableKeyUp) {
    // handle keyup event behavior
  }
})

We are using a boolean variable to disable the keyup behavior because only when disableKeyUp is set to false , the keyup event behavior will happen.

Don't forget to re-enable the keyup behavior when you do need it :)

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