Javascript OnKeyPress事件中缺少最后一个字符

Was*_* A. 10 javascript-events onkeypress

function h(x)
{
    alert(x);
}

<input onkeypress=h(this.value) type=text>
Run Code Online (Sandbox Code Playgroud)

当我
在'a'='ab'之后按'b' 时按'a'警告为空时只有'a'我想要'ab'而
当我输入'abcd'时我想要'ab '它只警告'abc'而我想要'abcd'

Lix*_*xas 20

您的活动信件注册触发.你应该使用onkeyup事件.释放密钥后,它会启动

  • 仅供参考:如果用户按住某个键,`onkeyup` 将不会注册多次按下 (2认同)

Was*_* A. -2

var unicode=e.keyCode? e.keyCode : e.charCode;
typing = document.getElementById('textbox').value + String.fromCharCode(unicode);
Run Code Online (Sandbox Code Playgroud)

  • 废话。你的问题和你的答案。有偏见。 (3认同)