[英]How can I press one of keyboard's button by jQuery?
Here is my code: 这是我的代码:
$("input").on({ keydown: function(ev) { if (ev.which === 27){ // esc button backspace_emolator(); } else if (ev.which === 8){ // backspace button console.log('backspace button pressed'); } } }); function backspace_emolator(){ // remove one character exactly how backspace button does }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="fname" class="myinput">
All I'm trying to do is making esc button exactly the same as backspace button. 我要做的就是使esc按钮与Backspace按钮完全相同。 So I want to remove one character of .myinput
's value when <kbd>esc</kbd>
is pressed. 所以我想在按下<kbd>esc</kbd>
时删除.myinput
值的一个字符。
How can I do that? 我怎样才能做到这一点?
Note: I don't want to just remove one character of .myinput
's value. 注意:我不想只删除.myinput
值的一个字符。 I want to press backspace button. 我想按退格键。 So backspace button pressed
should be shown in the console when I press esc button. 因此,当我按下esc按钮时,应该在控制台中显示backspace button pressed
按钮。
This is what I would do. 这就是我要做的。 I do not argue that this is the best solution, instead it is a working one. 我不认为这是最佳解决方案,而是可行的解决方案。
$("input").on({ keydown: function(ev) { if (ev.which === 27){ // esc button backspace_emolator(this); } else if (ev.which === 8){ // backspace button console.log('backspace button pressed'); } } }); function backspace_emolator(el){ // remove one character exactly how backspace button does var val = el.value el.value = val.substring(0, val.length - 1); // trigger event var e = jQuery.Event("keydown"); e.which = 8; // # Backspace keyCode $(el).trigger(e); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="fname" class="myinput">
Edited: I have modified my code in respect to your edit. 编辑:我已经根据您的修改修改了我的代码。 The reason @Alexandru-Ionut Mihai suggestion does not work for you is because you are only listening for keydown and in his example he is triggering keypress. @ Alexandru-Ionut Mihai的建议对您不起作用的原因是,您只在听按键按下,而在他的示例中,他正在触发按键。
Here is solution: 这是解决方案:
You have to trigger
an event for your input
. 您必须为input
trigger
一个事件。
$("input").on({
keydown: function(ev) {
if (ev.which === 27){ // esc button
backspace_emolator();
}
}
});
function backspace_emolator(){
var e = jQuery.Event("keydown", { keyCode: 8 }); // you can specify your desired key code
$('input').trigger(e);
}
Use this code it works as you want it to be. 使用此代码,即可按需使用。
$("input").on({ keydown: function(ev) { if (ev.which === 27){ // esc button backspace_emolator(ev); } if (ev.which === 8){ // backspace button console.log('backspace button pressed'); } } }); function backspace_emolator(ev){ // remove one character exactly how backspace button does var inputString = $('#inputID').val(); var shortenedString = inputString.substr(0,(inputString.length -1)); $('#inputID').val(shortenedString); console.log("Backspace button Pressed"+" "+inputString); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="inputID" type="text" name="fname" class="myinput">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.