jQuery:输入时更改键盘值 [英] jQuery: Change keyboard value when input

查看:133
本文介绍了jQuery:输入时更改键盘值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:当我按下键盘上的A键时,该值将变为S,文本框显示为S。

Example: when I press key "A" on a keyboard, then the value will change to "S" and text box show up "S".

如何我可以用jQuery或Tinymce 4做这个吗?

How can I do this with jQuery or Tinymce 4?

非常感谢!

推荐答案

您需要在每个 keypress 事件中检查 keyCode 。如果 keyCode A 相同,那么您需要将其更改为您想要的任何字符串,例如: S

You will need to check the keyCode in every keypress event. If the keyCode is the same with A then you need to change it to whatever string you want, for example: S.

<div>
    <label>Test:</label>
    <input type="text" id="test">
</div>

$("#test").on("keypress", function (e) {
    if (97 == e.keyCode || 65 == e.keyCode) {
        e.preventDefault();
        var newString = $("#test").val() + "S";
        $("#test").val(newString);
    }
});

97 是<$ c的密钥代码$ c> a 和 65 A 的密钥代码。

a 将不会显示在 e.preventDefault(); 的框中

小提琴是这里

这篇关于jQuery:输入时更改键盘值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆