jQuery的键盘功能不工作? [英] jQuery keyup function doesnt work?
问题描述
我的HTML档案:
< html>
< head>
< script type =text / javascriptsrc =js / jquery-1.10.2.min.js>< / script>
< script type =text / javascriptsrc =js / scripts.js>< / script>
< link rel =stylesheettype =text / csshref =style.css/>
< title>
登入
< / title>
< / head>
< body>
< div class = loginForm>
< p> Worker-ID:< input type = text id = workerID name = workerID />< / p>
< p>密码:< input type = password id = workerPassword name = workerPassword />< / p>
< input type = submit id = submitLogin name = submitLogin value =Log in/>
< / div>
< / body>
< / html>
我的scripts.js:
<$ p $ ('key');
)$('#workerID')。keyup(function(){
alert('key up');
);
完全不起作用。我尝试了一切空间,一个字母,数字。该警报不显示。错误在哪里?
除了你缺少的}
,当 script.js
文件运行时(在< head>
部分中),文档的其余部分不存在。解决这个问题的最简单方法是将脚本包装在文档就绪处理程序中,例如
jQuery(function($){ ('keyup',function(){
alert('key up');
});
});
$('#workerID')。
或者,您可以将脚本移动到文档的底部,例如
< script src =js / scripts.js>< / script>
< / body>
< / html>
或者使用事件代理允许您将事件绑定到父元素(或文档),例如
$ b $ pre $
$(document).on('keyup','#workerID',function(){
alert( 'key up');
});
My HTML file:
<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:
$('#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?
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
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');
});
这篇关于jQuery的键盘功能不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!