jQuery keyup函数不起作用?

adi*_*ii4 7 html javascript jquery keyup

我的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>
Run Code Online (Sandbox Code Playgroud)

我的scripts.js:

$('#workerID').keyup(function() {
    alert('key up');
);
Run Code Online (Sandbox Code Playgroud)

它根本不起作用.我尝试了所有空间,一个字母,数字.警报未显示.哪里出错了?

Phi*_*hil 26

除了遗失的错误之外},当script.js文件运行时(在该<head>部分中),文档的其余部分不存在.解决此问题的最简单方法是将脚本包装在文档就绪处理程序中,例如

jQuery(function($) {
    $('#workerID').on('keyup', function() {
        alert('key up');
    });
});
Run Code Online (Sandbox Code Playgroud)

或者,您可以将脚本移动到文档的底部,例如

        <script src="js/scripts.js"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

或者使用事件委托,它允许您将事件绑定到父元素(或文档),例如

$(document).on('keyup', '#workerID', function() {
    alert('key up');
});
Run Code Online (Sandbox Code Playgroud)