如何解码从jQuery的keydown()的事件处理程序的字符 [英] How to decode character pressed from jQuery's keydown()'s event handler
问题描述
我需要弄清楚从jQuery的 keydown
函数调用的处理程序中输入的文本字段中输入了哪个字符。 key.which
只给我keycode,但我需要弄清楚哪个ASCII字符 key
表示。
I need to figure out which character was typed into a text field from within the handler that is called by jQuery's keydown
function. key.which
gives me only the keycode, but I need to figure out which ASCII character key
represents. How do I do this?
推荐答案
对于字符输入,建议您使用 keypress / code>,它将报告按下的字符的实际ASCII代码。它自动处理大写字母,忽略非字符印刷。在任一情况下,您都可以使用fromCharCode()转换为字符串表示形式。例如
For character input, it is suggested you use keypress()
, which will report the actual ASCII code for the character pressed. It automatically takes care of letter case, and ignores non-character presses. In either case, you can use fromCharCode() to convert to a string representation. E.g.
var c = String.fromCharCode(e.which) // or e.keyCode
请记住, keydown()
和 )
,您必须使用 e.shiftKey
状态跟踪案例。
Just remember that for keydown()
and keyup()
, you'll have to keep track of the case using the e.shiftKey
state.
这篇关于如何解码从jQuery的keydown()的事件处理程序的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!