简体   繁体   中英

Ruby on Rails Ajax Checkbox

How can I bind a click event to a column of checkboxes in a table?

- @foos.each do |foo|
  = check_box_tag 'checkbox', foo.active, foo.active

:javascript 
$(document).ready(function() {
  var $this = document.getElementById('active');
  if($this.checked == true){
    $this.bind("click", function(){ alert("True");});
  } else if (document.getElementById('active').checked == false) {
    $this.bind("click", function(){ alert("False");});
  }
});
  $("input:checkbox[name=checkbox]").each(function(){
    var $this = $(this);
    $this.bind("click", function(){ alert($this.is(":checked")); });
  });

you had a wrong selector for jQuery, use this:

:javascript 
  $(document).ready(function() {
    $("input[type=checkbox]").each(function(){
      $(this).bind("click", function(){ alert($(this).is(":checked")); });
    });      
  });

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