Jquery .change()函数在localhost上不起作用

时间:2013-02-25 12:21:10

标签: jquery input controls textchanged

以下代码适用于jsfiddle,但不适用于localhost,你能帮帮我吗?

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script>
        $('.puankontrol').change(function() {
          var x = $(this).val() * 1;   
          if((x < 1) ||  (x > 5))
          {
              $(this).val(3);
              alert("Please enter value between 1-5!");
          }
        });
    </script>
</head>
<body>

     <form>
      <input class="puankontrol" type="text" name="puan"/><BR>
      <input class="puankontrol" type="text" name="puan"/><BR>
      <input class="puankontrol" type="text" name="puan"/><BR>
    </form>
    <div id="other">
      Trigger the handler
    </div>

</body>
</html>

`

2 个答案:

答案 0 :(得分:7)

尝试

$(document).ready(function(){
   //change function declaration.
});

还可以使用<script type="text/javascript">添加script代码。

答案 1 :(得分:0)

每当你想在jQuery中编写任何代码时,你必须在

中添加该代码
$(document).ready(function(){});

一旦加载了文档的所有元素,jQuery就会调用此方法回调。 并确认我们在其中访问的所有元素都已加载。