1
我想让复选框检查所有的子复选框。为什么我的复选框不起作用?我正在使用codeigniter,这里是我的视图
我的复选框:
<table width="30%" class="table striped hovered cell-hovered border bordered">
<tr valign="middle">
<td><b>IDPEL</b></td>
<td><b>No. Baris</b></td>
<td><b><input type="checkbox" id="pilihsemua"/> Pilih Semua</b></td>
</tr>
<?php
foreach ($panel_error as $key) {
echo"<tr><td>".$key->errpanel."</td>";
echo"<td>".$key->nomorBaris."</td>";
echo"<td>";
echo form_checkbox('chk_boxes1[]',$key->errpanel);
echo"</td></tr>";
}
?>
</table>
,这里是我的脚本:
<script type="text/javascript">
$(document).ready(function() {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',checked)
})
$('#table1').dataTable();
$('#table2').dataTable();
//checkbox
$("#pilihsemua").click(function() { // If #pilihsemua checked, all checkbox will be checked.
$('.chk_boxes1[]').attr('checked', checked);
});
// if all sub checkboxes are being checked, #pilihsemua will automatically checked.
$(".chk_boxes1[]").click(function(){
if($(".chk_boxes1[]").length == $(".chk_boxes1[]:checked").length) {
$("#pilihsemua").attr("checked", "checked");
} else {
$("#pilihsemua").removeAttr("checked");
}
});
//end of checkbox
});
</script>
不过,我仍然不知道为什么,就不能工作。我尝试检查#pilihsemua但所有的子类不检查。或者如果我检查了所有的子类,#pilihsemuadoes不会被检查。
类名不附带'[]'。你在jQuery中使用名字。 – Yash
检查[这个小提琴](http://jsfiddle.net/NogginBox/ScnQT/1/)和[这个堆栈](http://stackoverflow.com/questions/18537609/jquery-checkbox-check-all)链接 – PHPExpert
哇,真的有帮助!非常感谢! – Berlian