jQuery keyUp永远不会触发

时间:2015-04-19 12:15:36

标签: javascript jquery html keyup

简单代码:

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

    <script type="text/javascript" src="http://code.jquery.com/jquery-    
     latest.min.js"></script>
    <script type="text/javascript">
        $("#target").keyup(function() {
            alert("Handler for .keyup() called.");
        });
    </script>
</head>

<body>
    <form>
        <input id="target" type="text">
    </form>
</body>

</html>

我可以输入输入字段,但不会显示警告。我也在Chrome和Mozilla中试过这个。

1 个答案:

答案 0 :(得分:1)

您需要在文档完成加载后运行keyup函数

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

  <script type="text/javascript" src="http://code.jquery.com/jquery-    
 latest.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#target").keyup(function() {
        alert("Handler for .keyup() called.");
      });
    });
  </script>
</head>

<body>
  <form>
    <input id="target" type="text">
  </form>
</body>

</html>