views:

2970

answers:

2

Hello everyone, 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. need help. thanks!

+1  A: 

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

John Boker
This is just one example. :)
A: 

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    
    // ...
  }
});
eKek0
Thank you, I know this method, but I need use change event. In addition, this problem exists only in IE6.