gam*_*h00 1 javascript jquery alert enter
在Stackoverflow中阅读此帖后,(检测文本输入字段中的Enter键)我试着让我的一些输入触发输入...我没有工作.我做了一个简单的例子,它仍然无效.有人可以暗示正确的方式吗?(仅供参考 - 绝对是新手)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>On Enter</title>
<script src="../../Scripts/jquery-2.1.0.min.js"></script>
<script>
$(".input1").keyup(function (e) {
if (e.keyCode == 13) {
alert ("It Works");
}
});
</script>
</head>
<body>
<input type="text"
id="input1"
placeholder="Press Enter When Done">
</input>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
谢谢
首先你需要说的#input1不是.input1,然后在整个DOM准备就绪时运行它:
$(document).ready(function(){
$("#input1").keyup(function (e) {
if (e.keyCode == 13) {
alert ("It Works");
}
});
});
Run Code Online (Sandbox Code Playgroud)
编辑:正如在评论中提到的@jfriend00一样,使用e.which其他的是个好主意e.keycode.为此,您可以更改:e.keyCode == 13到e.which == 13.这种方式是由人jQuery的重新开始,因为它正常化e.keyCode和e.charCode.