【问题标题】:keyup, keydown and keypress events not working on mobilekeyup、keydown 和 keypress 事件在移动设备上不起作用
【发布时间】:2021-12-23 06:26:21
【问题描述】:

我一直试图让它工作,但我不知道发生了什么,我有代码:

$('#buscar-producto').on('keydown', function(e){
    console.log('hello');
    console.log(e.keyCode);
});

它可以在计算机上运行,​​但不能在移动设备上运行..

编辑: 我需要在按下某个键时获取 keyCode...

【问题讨论】:

标签: javascript jquery


【解决方案1】:

keydown 应该工作,但您可以使用input 事件,这似乎对 Android 移动设备产生了不良影响...
要获取按下的键的代码,请使用 jQuery 的规范化 Event.which

Android Chrome 已测试:

使用input 事件(e.which 总是给出0,所以这似乎是安卓设备上的一个错误)

jQuery(function($) { // DOM ready and $ alias secured

  $('#buscar-producto').on('input', function(e){
    var key = e.which || this.value.substr(-1).charCodeAt(0);
    alert( key )
  });

});
<input type="text" id="buscar-producto" placeholder="Buscar...">

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

使用keydown(按预期工作)

jQuery(function($) { // DOM ready and $ alias secured

  $('#buscar-producto').on('keydown', function(e){
    alert( e.which );
  });

});
<input type="text" id="buscar-producto" placeholder="Buscar...">

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

【讨论】:

  • 我在我的问题中遗漏了一些内容,请您重新阅读一下吗? :)
  • 使用 input 如 Roko 提到的并使用 event.which 获取密钥代码。查看更多信息:api.jquery.com/event.which
  • keydown 现在已损坏。使用桌面模式在移动设备上运行此页面,以便 sn-p 可用,无论按什么,总是会给出 229。在桌面上,出现了正确的字符代码。有谁知道是什么原因造成的或解决方法是什么?
猜你喜欢
  • 2018-02-20
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2011-03-23
相关资源
最近更新 更多