键,键按下和键事件在移动设备上不起作用 [英] keyup, keydown and keypress events not working on mobile

查看:56
本文介绍了键,键按下和键事件在移动设备上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使它起作用,但是我不知道发生了什么,我拥有的代码是

I have been trying to get this to work but I don't know what's going on, the code I have:

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

它可以在计算机上使用,但不能在移动设备上使用..

It works on computers, but not on mobile..

按下键时我需要获取keyCode ...

I need to get the keyCode when a key is pressed...

推荐答案

keydown 应该有效,但是您可以使用input事件,该事件似乎在Android移动设备上产生了不良影响. .
要获取按下键的 code ,请使用jQuery的规范化的Event.which

keydown should work, but you could use the input event which seems to have undesired effects on Android mobile...
To get the code of the pressed key use the jQuery's normalized Event.which

Android Chrome 已测试:

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>

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>

这篇关于键,键按下和键事件在移动设备上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆