开发者

Javascript, key press value is always one character behind the latest?

开发者 https://www.devze.com 2023-01-11 18:10 出处:网络
If I type \'St\', by the time I press the t, if I output the input of textfield.value in the onkeypress/onkeydown functions, I开发者_Go百科 only get \'S\'.

If I type 'St', by the time I press the t, if I output the input of textfield.value in the onkeypress/onkeydown functions, I开发者_Go百科 only get 'S'.

Why is this? How do I get rid of this lag?


use the keyup event instead of keypress. keydown will show the before-keystroke value, as will keypress (apparently).


Within the keypress event, it's still possible to prevent the typed character from registering, so the input's value canot be updated until after the keypress event. You can use the keyup event instead, or use window.setTimeout() to set up a delay.


Because the keystroke is not registered until keyup event occurs. So you should detect onkeyup event instead of onkeypress.

0

精彩评论

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

关注公众号