jquery change event trigger

三世轮回 提交于 2019-12-13 20:30:43

问题


I have a problem in need of help of JQuery. JQuery give my input to increase the use of case change. the code:

#(".inp").bind("change",function (){
  if(isNaN(this.value)){
    this.value = 0;
  }
}

When I first time to enter characters, change case happened, input values changed to 0 But when I enter the same character a second time, change does not happen.


回答1:


From jQuery documentation:

The change event fires when a control loses the input focus and its value has been modified since gaining focus.

So, it it fired only once. And that's why you have the behaviour you got. I would use the KeyDown event, because

The keydown event fires when a key on the keyboard is pressed.

To use this event you have to do something like this:

$('.inp').keydown(function(event){
  switch (event.keyCode) {
    // ...
    // different keys do different things
    // Different browsers provide different codes
    // see here for details: http://unixpapa.com/js/key.html    
    // ...
  }
});



回答2:


It looks like you might want to use the keypress event, not the change one.



来源:https://stackoverflow.com/questions/695946/jquery-change-event-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!