我一直试图让这个工作起来,但我不知道发生了什么,我的代码:
$('#buscar-producto').on('keydown', function(e){
console.log('hello');
console.log(e.keyCode);
});
它适用于计算机,但不适用于移动设备..
修改 我需要在按下某个键时获取keyCode ...
答案 0 :(得分:2)
keydown
应工作,但您可以使用似乎对Android手机产生不良影响的input
事件...
要获取按下的键的代码,请使用jQuery的规范化Event.which
Android Chrome 已经过测试:
input
事件(e.which
始终提供0
所以它似乎是Android设备上的错误)
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>