I have an input element with an onkeypress event that runs a function, change(). The problem is that the function doesn't run for the very first keypress. For example, I have to type 55 instead of 5 to get the function to update, and the value it takes is 5. Somehow, it doesn't recognize the most recent keypress. I tried adding other events, such as onfocus and onselect, to no avail. Here is the code for the input:

<input id="x" type="text" size="1" maxlength="2" onkeypress="change()" onfocus="change()" onselect="change()" onblur="change()" onchange="change()"></input>

JSFiddle

有帮助吗?

解决方案

I think the issue might be that the onkeypress event triggers before the value is inserted.

If you want to capture the value after the key has been pressed you could use the onkeyup event instead and see if that works better.

其他提示

you might to try this event

onKeyUp="change()"

link no longer valid

I guess it is because the value is just set after you release the key. When I try it with onkeyup its working

http://jsfiddle.net/j8B8b/2/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top