jQuery: Change keyboard value when input

被刻印的时光 ゝ 提交于 2019-12-08 12:15:35

问题


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

How can I do this with jQuery or Tinymce 4?

Thank you very much!


回答1:


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 is the keycode for a and 65 is keycode for A.

a will not be shown in the box with e.preventDefault();

Fiddle is here.



来源:https://stackoverflow.com/questions/38236831/jquery-change-keyboard-value-when-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!