How can I get jquery .val() AFTER keypress event?

后端 未结 6 1736
北荒
北荒 2020-11-28 06:32

I got:

$(someTextInputField).keypress(function() {
  alert($(this).val());
});

Now the alert always returns the value BEFORE the keypress (

6条回答
  •  星月不相逢
    2020-11-28 07:25

    You may want to hook up an onchange event handler instead of using any of the key events.

    $(someTextInputField).change(function() {
        alert($(this).val());
    });
    

    Using Edit > Paste or a Right-Click then Paste will fire a change event, but not a key event. Note some browsers may simulate a key event on a paste (though I don't know of any) but you can't count on that behavior.

提交回复
热议问题