jQuery:keyPress Backspace 不会触发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4690330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:44:12  来源:igfitidea点击:

jQuery: keyPress Backspace won't fire?

jquerykeypressbackspace

提问by matt

I wonder what I'm doing wrong:

我想知道我做错了什么:

$(".s").keypress(function(e) {
   switch (e.keyCode) {
      case 8: // Backspace
      //console.log('backspace');
      case 9: // Tab
      case 13: // Enter
      case 37: // Left
      case 38: // Up
      case 39: // Right
      case 40: // Down
         break;

      default:
         doSearch();
   }
});

I want my doSearch()function also to be fired when I hit the Backspacekey. At the moment absolutely nothing happens when I press Backspacein Chrome and Safari.

我希望我的doSearch()功能也能在我按下Backspace键时被触发。目前,当我Backspace在 Chrome 和 Safari 中按下时绝对没有任何反应。

any ideas?

有任何想法吗?

回答by Jonathon Bolster

Use keyupinstead of keypress. This gets all the key codes when the user presses something

使用keyup代替keypress。当用户按下某物时,它会获取所有的键码

回答by Rob

I came across this myself. I used .onso it looks a bit different but I did this:

我自己遇到了这个。我用过.on所以它看起来有点不同,但我这样做了:

 $('#element').on('keypress', function() {
   //code to be executed
 }).on('keydown', function(e) {
   if (e.keyCode==8)
     $('element').trigger('keypress');
 });

Adding my Work Around here. I needed to delete ssn typed by user so i did this in jQuery

在此处添加我的工作。我需要删除用户输入的 ssn 所以我在 jQuery 中做了这个

  $(this).bind("keydown", function (event) {
        // Allow: backspace, delete
        if (event.keyCode == 46 || event.keyCode == 8) 
        {
            var tempField = $(this).attr('name');
            var hiddenID = tempField.substr(tempField.indexOf('_') + 1);
            $('#' + hiddenID).val('');
            $(this).val('')
            return;
        }  // Allow: tab, escape, and enter
        else if (event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
        // Allow: Ctrl+A
        (event.keyCode == 65 && event.ctrlKey === true) ||
        // Allow: home, end, left, right
        (event.keyCode >= 35 && event.keyCode <= 39)) {
            // let it happen, don't do anything
            return;
        }
        else 
        {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) &&       (event.keyCode < 96 || event.keyCode > 105)) 
            {
                event.preventDefault();
            }
        }
    });

回答by Marcos Nunes

If you want to fire the event only on changes of your input use:

如果您只想在输入更改时触发事件,请使用:

$('.s').bind('input', function(){
  console.log("search!");
  doSearch();
});

回答by Halcyon

According to the jQuery documentation for .keypress(), it does not catch non-printable characters, so backspace will not work on keypress, but it is caught in keydown and keyup:

根据 .keypress() 的 jQuery 文档,它不会捕获不可打印的字符,因此退格键在 keypress 上不起作用,但会在 keydown 和 keyup 中捕获:

The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, except that modifier and non-printing keys such as Shift, Esc, and delete trigger keydown events but not keypress events. Other differences between the two events may arise depending on platform and browser. (https://api.jquery.com/keypress/)

当浏览器注册键盘输入时,keypress 事件被发送到一个元素。这类似于 keydown 事件,不同之处在于修饰符和非打印键(例如 Shift、Esc 和 delete 会触发 keydown 事件而不是 keypress 事件)。根据平台和浏览器的不同,可能会出现两个事件之间的其他差异。( https://api.jquery.com/keypress/)

In some instances keyup isn't desired or has other undesirable effects and keydown is sufficient, so one way to handle this is to use keydownto catch all keystrokes then set a timeout of a short interval so that the key is entered, then do processing in there after.

在某些情况下,不需要 keyup 或有其他不良影响,并且 keydown 就足够了,因此处理此问题的一种方法是使用keydown捕获所有击键,然后设置一个短时间间隔的超时,以便输入密钥,然后进行处理在那里之后。

jQuery(el).keydown( function() { 
    var that = this; setTimeout( function(){ 
           /** Code that processes backspace, etc. **/ 
     }, 100 );  
 } );