简体   繁体   中英

Popup is triggered multiple times with short time interval

I wanted to make an input field to react to invalid inputs dynamically in such a way that a popup (with window.alert) will be displayed to inform the players of the invalid input. However, after I close the popup, the very same popup will be displayed twice with very short time interval between. The input can be changed after this though.

How can I solve it? Would there be a way to make sure that the popup is not displayed again for a couple of second?

Relevant code:

$(':input').bind('keypress keydown keyup change',function(){
  var weight = parseFloat(String($(':input[name='.concat(n,']')).val()).replace(',', '.'));

  if (weight > 100 || weight < 0){
      window.alert("Bitte eine Nummer zwischen 0 und 100 eingeben!");
  }

});

You are binding 4 events to do the same thing, so you should get 4 alerts:

All 4 event fires because you press the key, and release the key, and since the element looses focus when you do the alert, also the change-event is fired.

  • keypress (Deprecated) fired when a key that produces a character value is pressed down
  • keydown is fired when a key is pressed.
  • keyup is fired when a key is released.
  • change is fired when value is committed by the user.

You forgot the input -event to be complete...

Use the change-event, to only run your code when the value is committed by the user.

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