我一直在尝试让它工作,但我不知道发生了什么事,以下是我的代码:
$('#buscar-producto').on('keydown', function(e){
console.log('hello');
console.log(e.keyCode);
});
它可以在电脑上运行,但在移动设备上无法运行。
编辑: 我需要在按键时获取keyCode...
我一直在尝试让它工作,但我不知道发生了什么事,以下是我的代码:
$('#buscar-producto').on('keydown', function(e){
console.log('hello');
console.log(e.keyCode);
});
它可以在电脑上运行,但在移动设备上无法运行。
编辑: 我需要在按键时获取keyCode...
keydown
事件应该可以使用,但在Android移动设备上使用事件可能会产生不良影响... 为了获取按下键的代码,请使用jQuery的标准化Event.which
。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>
input
,并使用event.which
来获取按键代码。更多信息请参见:https://api.jquery.com/event.which/ - bamtheboozleevent.key
、event.keyCode
、event.which
。event.target.value.splice(-1)
。splice
出了问题,我不得不改用 slice(-1)
。 - undefined