简体   繁体   中英

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. I also tried on('change') and click events - they're not working either. By the way, I'm using 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

$(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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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