事件处理程序按下的字符

事件处理程序按下的字符

本文介绍了如何解码从 jQuery 的 keydown() 事件处理程序按下的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定在 jQuery 的 keydown 函数调用的处理程序中将哪个字符输入到文本字段中.key.which 只给我键码,但我需要弄清楚 key 代表哪个 ASCII 字符.我该怎么做?

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(),它会报告所按下字符的实际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()keyup(),您必须使用 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() 事件处理程序按下的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:39