简体   繁体   English

jQuery如何按键盘按钮之一?

[英]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.

相关问题 我怎样才能改变顺序<li>当我在<button>不使用 JQuery 的情况下使用 JS</button>按下 a 时,s 在列表中<button>?</button> - How can I change the order of <li>s in a list when I press a <button> with JS without using JQuery? 使用 JQuery 按键盘上的 End 按钮 - Press End button on keyboard using JQuery 如何使用vanilla JavaScript在浏览器窗口中模拟键盘按下? - How can I emulate a keyboard press in a browser window with vanilla JavaScript? 如何使用堆栈导航器通过按一下按钮从一个屏幕导航到另一个屏幕? - How can I navigate from one screen to another via a button press using stack navigator? 即使按下键盘上的Enter键,如何调用按钮按下事件 - How to call a Button press Event even with the press of the Enter key on keyboard 如何使用javascript / jquery / php在Web应用程序的按钮单击上按下键盘快捷键 - How to press keyboard shortcuts on button click of web app using javascript/jquery/php 持续按下按钮的键盘 - Persist Keyboard on Button Press 如何将键盘笔划分配给按钮? - How can i assign a Keyboard stroke to a button? 如何使用JavaScript或jQuery中的键盘快捷键触发不同的按钮事件? - How can I trigger different button events with keyboard shortcuts in JavaScript or jQuery? 需要按两次按钮,首先关闭键盘,然后发送。 如何停止呢? - Need to press button twice, first to close keyboard second to send. How do I stop this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM