如何使用 jQuery 检测表单输入框中按下的键?
jqueryweb developmentfront end technology
要使用 jQuery 检测表单输入框中按下的键,可以使用 jQuery 的 keydown 事件。您可以尝试运行以下代码来学习如何检测表单输入框中的按键。
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/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>

