问题
function h(x)
{
alert(x);
}
<input onkeypress=h(this.value) type=text>
when i press 'a' alert empty
when i press 'b' after 'a' =>ab alert only 'a' and i want 'ab'
when i type 'abcd' it alert 'abc' only and i want 'abcd'
回答1:
your event fires before letter is registered. you should use onkeyup event. It kicks-in after you release key
回答2:
Javascript:
function h(x)
{
alert(x);
}
HTML Code:
<input onkeyup=h(this.value) type=text>
回答3:
var unicode=e.keyCode? e.keyCode : e.charCode;
typing = document.getElementById('textbox').value + String.fromCharCode(unicode);
来源:https://stackoverflow.com/questions/5340751/javascript-last-character-missing-on-onkeypress-event