简体   繁体   English

jQuery 更改事件不适用于复选框

[英]jQuery change event is not working with checkbox

<tr>
  <td class="large-width">
    <p>
      <label>
        <input type="checkbox" id="checkbox" ${todo.isCompleted ? 'checked="checked"' : ''} />
        <span class="text">${todo.item}</span>
      </label>
    </p>
  </td>
</tr>

<script>
   $('#checkbox').change(function(){
     console.log('something');
   });
</script>

I can see that checkbox is changing it's state, but Change event is not working.我可以看到复选框正在更改它的 state,但更改事件不起作用。 I also tried on('change') and click events - they're not working either.我也尝试过('change')和点击事件——它们也不起作用。 By the way, I'm using materializecss.顺便说一句,我正在使用materializecss。

Keep it inside document ready把它放在文件里面准备好

<script>
$(function(ready){
   $('#checkbox').change(function(){
     console.log('something');
   });
});
</script>

Need to call below jQuery methods to working code需要调用下面的 jQuery 方法来工作代码

$(document).ready(function(){ // jQuery methods go here... });

OR或者

$(function(){ // jQuery methods go here... });

 <tr> <td class="large-width"> <p> <label> <input type="checkbox" id="checkbox" ${todo.isCompleted? 'checked="checked"': ''} /> <span class="text">${todo.item}</span> </label> </p> </td> </tr> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script>$(document).ready(function(){ $('#checkbox').change(function(){ console.log('something'); }); }); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM