获取在
我想知道是否有办法获取 keydown 或 keypress 事件上 a 的当前值。为了以更好的方式定义“当前值”,我的意思是文本区域的值,包括“刚刚插入”的字符。默认情况下,这些事件的值不会更改。如果这是不可能的,我想知道是否有一种跨浏览器方法来获取我按下的键的刚刚插入的值(我不需要keycode,因为例如这没有定义,假设我输入一个字符(如果输入的字符是大写或小写)。
I was wondering if there is a way to get the current value of a on the keydown or the keypress event. To define "current value" in a better way, I mean, by this, the value of the textarea, including the "just inserted" character. By default on these events the value does not change. If this is not possible i would like to know if there is a cross browser way to get the just inserted value of the key that I pressed (I don't need the keycode, because e.g. this does not define, supposing that I enter a characterm if the character entered is Uppercase or Lowercase).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在实际值更改之前,您必须等待 keyup 事件触发。
否则,对于 keydown 或 keypress,您必须在事件上映射字符代码(每个浏览器都不同,除非您使用一些 JS 库(如 jQuery 对其进行标准化))并确定光标位置并以这种方式修改值。除非您使用 JS 库来执行此操作,否则这可能会有点棘手,尤其是在浏览器支持方面。
You'll have to wait for the keyup event to fire before the actual value changes.
Otherwise, for keydown or keypress, you have to map the character code on the event (and this is different per browser unless you use some JS library like jQuery which standardizes it) and determine the cursor position and modify the value that way. This can get a little tricky especially around browser support unless you use a JS library to do this.
我编写了一个模块,将
keypress
、keydown
和keyup
事件分别转换为字符和按键。 https://github.com/fresheneesz/keysight示例:
I wrote a module that translates
keypress
,keydown
, andkeyup
events into characters and keys respectively. https://github.com/fresheneesz/keysightExample: