简体   繁体   中英

Alert disappear with keyup event

I'm binding two events to the alert function: on the click of a button and the press of button space. The function shuffle display an alert, and I want to either click button or press space to show the dialog ( for a long time).

  $("#ShuffleButton").on("click", Shuffle);

  $(window).keypress(function(e) {
    e.preventDefault();
    if (pressAllowed) {
      pressAllowed = false;

      if (e.keyCode == 0 || e.keyCode == 32) {
        Shuffle();  
      }          
    }
  });

  $(window).keyup(function(e) {
    e.preventDefault();
    pressAllowed = true;
  });      
}

When I click the button, it works fine. But when I press space, the alert shows up and immediately disappear when keyup event is triggered. What is the reason for this?

I've tested this out in Chrome - pressing the space bar clicks the Ok button on an alert. Your keypress is bringing up the alert window, and then immediately closing it as well. You can test this by trying a keycode other than 32 - the alert window will come up and not disappear.

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