要知道使用jQuery在表单输入中按下了哪个键,请使用jQuery keydown事件。您可以尝试运行以下代码以了解如何检测在表单输入中按下的键-
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#myinput').keydown(function(e) {
var newkey = 'Key code = ' + e.which + ' ' + (e.ctrlKey ? 'Ctrl' : '') + ' ' + (e.shiftKey ? 'Shift' : '') + ' ' + (e.altKey ? 'Alt' : '');
$('#mykey').text(newkey);
return false;
});
});
</script>
</head>
<body>
<form id="myform">
Press any key: <input id='myinput' type='text' />
</form>
<div id='mykey'>The key you press is visible here with keycode.</div>
</body>
</html>