jQuery 按键功能不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19373455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery keyup function doesnt work?
提问by adiii4
My HTML file:
我的 HTML 文件:
<html>
<head>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/scripts.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>
Login
</title>
</head>
<body>
<div class=loginForm>
<p>Worker-ID:<input type=text id=workerID name=workerID /></p>
<p>Password:<input type=password id=workerPassword name=workerPassword /></p>
<input type=submit id=submitLogin name=submitLogin value="Log in"/>
</div>
</body>
</html>
My scripts.js:
我的scripts.js:
$('#workerID').keyup(function() {
alert('key up');
);
It doesn't work at all. I tried everything space,one letter, numbers. The alert doesn't show up. Where is the mistake?
它根本不起作用。我尝试了所有空间,一个字母,数字。警报不显示。错误在哪里?
回答by Phil
Apart from a typo around your missing }
, when your script.js
file runs (in the <head>
section), the rest of your document does not exist. The easiest way to work around this is to wrap your script in a document ready handler, eg
除了丢失的错字外}
,当您的script.js
文件运行(在该<head>
部分中)时,文档的其余部分不存在。解决此问题的最简单方法是将脚本包装在文档就绪处理程序中,例如
jQuery(function($) {
$('#workerID').on('keyup', function() {
alert('key up');
});
});
Alternatively, you could move your script to the bottom of the document, eg
或者,您可以将脚本移动到文档的底部,例如
<script src="js/scripts.js"></script>
</body>
</html>
or use event delegation which allows you to bind events to a parent element (or the document), eg
或使用允许您将事件绑定到父元素(或文档)的事件委托,例如
$(document).on('keyup', '#workerID', function() {
alert('key up');
});
回答by rink.attendant.6
You're missing the curly bracket to close the function:
您缺少用于关闭函数的大括号:
$('#workerID').keyup(function() {
alert('key up');
});
Errors like these are usually seen in the browser's JavaScript console.
像这样的错误通常出现在浏览器的 JavaScript 控制台中。
回答by Rituraj ratan
in HTML
在 HTML 中
inser id, name,vale in " "
插入 ID、名称、插入值 " "
<div class="loginForm">
<p>Worker-ID:<input type="text" id="workerID" name="workerID" /></p>
<p>Password:<input type="password" id="workerPassword" name="workerPassword" /></p>
<input type="submit" id="submitLogin" name="submitLogin" value="Log in"/>
</div>
in js
在js中
$('#workerID').keyup(function() {
alert('key up');} // here you forget "}"
);