Javascript Last character missing on OnKeyPress event

倖福魔咒の 提交于 2019-12-08 15:46:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!