开发者

Why does the "keypress" event handler not fire consistently?

I am having a problem with this fun开发者_开发问答ction. I cannot get it to fire properly. It does work, but when I highlight the quantity in the form and change the value (the value is initially set to 1), my stock level is 20, when I type in 21 it won't display the message unless I press another key. What am I doing wrong? I have tried every event possible, and can't figure out what I am doing wrong. BTW, this is in a jQuery UI dialog box.

$('#qty').live('keypress', function() {
  $('#response').hide();
  if($('#qty').val() > stockLevel) {
    var response = 'You selected more than we have in stock, we have reset your quantity to the maximum number available in stock';
    $('#qty').val(stockLevel);
    $('#response').html(response).show('blind', {}, '200'); 
    return false;
 }
});

Thanks for you help.


The keypress event has notoriously inconsistent behavior (because it is not defined in any standards). In your case your event is firing before the browser puts the text into the textbox. Use the keyup event instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜