Mar*_*ahn 2 javascript javascript-events
简单的问题——有没有人知道一个可靠的跨浏览器函数来从 keydown 事件中输入字符?我可以从 quirksmode 网格中编写一个,但宁愿不为如此简单但如此非标准的东西重新发明轮子。
让我澄清一下:
仅使用event.keyCode或无法做到这一点event.which。整体逻辑是这样的:
{186 : ';', 187 : '=', 188 : ',', ....}String.fromCharCode(key)这不是一个简单的解决方案,这就是为什么我正在寻找一个预制的解决方案:)
你愿意使用jQuery吗?
$("input").bind("keydown",function(e){ var value = this.value + String.fromCharCode(e.keyCode); }
Run Code Online (Sandbox Code Playgroud)
我相信切换到 keypress 事件会解决您评论中提到的问题。下面的代码是否符合您的要求?
$("input").bind("keypress",function(e){ var value = this.value + String.fromCharCode(e.keyCode); }
Run Code Online (Sandbox Code Playgroud)