如何获取jquery .val()按键事件之后? [英] How can I get jquery .val() AFTER keypress event?
问题描述
我有:
$(someTextInputField).keypress(function() {
alert($(this).val());
});
现在,警报总是返回按键之前的值(例如字段为空,我键入' '并且警报给了我'然后我键入'b',警报给我'一个'...)。但是我想要按键后的值 - 我该怎么做?
Now the alert always returns the value BEFORE the keypress (e.g. the field is empty, I type 'a' and the alert gives me ''. Then I type 'b' and the alert gives me 'a'...). But I want the value AFTER the keypress - how can I do that?
背景:一旦文本字段至少包含一个,我想启用一个按钮字符。所以我在每个按键事件上运行这个测试,但使用返回的val(),结果总是落后一步。使用change()事件对我来说不是一个选择,因为那个按钮被禁用,直到你离开文本框。如果有更好的方法,我很高兴听到它!
Background: I'd like to enable a button as soon as the text field contains at least one character. So I run this test on every keypress event, but using the returned val() the result is always one step behind. Using the change() event is not an option for me because then the button is disabled until you leave the text box. If there's a better way to do that, I'm glad to hear it!
推荐答案
更改 keypress
到 keyup
:
$(someTextInputField).on("keyup", function() {
alert($(this).val());
});
keypress
当钥匙被释放时, keyup
被触发。
keypress
is fired when the key is pressed down, keyup
is fired when the key is released.
这篇关于如何获取jquery .val()按键事件之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!