我的程序有问题.这是我的代码段.
这是Javascript / Jquery代码.
<script language='javascript'>
///SELECTING CHECKBoxES////
$(function(){
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkBox are selected, check the selectall checkBox
// and viceversa
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
</script>
这是我将集成该javascript的代码.
<h2>Quotation ID</h2>
<?PHP
$select_orders = MysqL_query ("SELECT * FROM tblorder WHERE project_id = '$project_id' GROUP BY quotation_id") OR DIE (MysqL_error());
while ($row2=MysqL_fetch_array($select_orders)){
$quote_id = $row2['quotation_id'];
?>
<h3 class="expand"><?PHP echo $quote_id; ?></h3>
<div class="collapse">
<table align='center' border='1' class='display'>
<thead>
<th><input type='checkBox' onclick='checkall()' id='selectall'/></th>
<th>Product Type</th>
<th width='20px'>Product type code</th>
<th width='20px'>Quantity</th>
<th>Width</th>
<th>Height</th>
<th>Total Sub.</th>
</thead>
<tbody>
<?PHP
$tots_tots = 0;
$tots_subs = 0;
$select_orders2 = MysqL_query ("SELECT * FROM tblorder WHERE project_id = '$project_id' AND quotation_id = '$quote_id'") OR DIE (MysqL_error());
while ($row3=MysqL_fetch_array($select_orders2)){
$idd = $row3['id'];
$project_id2 = $row3['project_id'];
$order_id = $row3['quotation_id'];
$prod_type = $row3['prod_type'];
$prod_type_code = $row3['prod_type_code'];
$qty = $row3['qty'];
$width = $row3['width'];
$height = $row3['height'];
$tot_sub = $row3['total_subs'];
$tots_subs += $tot_sub;
echo "<tr bgcolor='".$colors[$c++ % 2]."' align='center'>";
echo "<td>
<input type='hidden' name='project_name' value='$project_name'>
<input type='checkBox' name='check_ptc[]' value='$prod_type_code' style='display:none;' checked>
<input type='checkBox' class='case' name='checkBox[]' value='".$idd."'></td>";
echo "
<input type='hidden' name='project_id[]' value='$project_id2'>
</td>";
echo "<td>".$prod_type."
<input type='hidden' name='quotation_id[]' value='$order_id'>
<input type='hidden' name='prod_type[]' value='$prod_type'>
</td>";
echo "<td>".$prod_type_code."
<input type='hidden' name='prod_type_code[]' value='$prod_type_code'>
</td>";
echo "<td>".$qty."</td>";
echo "<td>".$width."</td>";
echo "<td>".$height."</td>";
echo "<td>".$tot_sub."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td></td><td></td><td></td><td></td><td></td>
<td>
<strong><b>Total:</b></strong>";
echo "</td>";
echo "<td>
<font color='#900'><u><b>$tots_subs</b></u></font>
</td>";
echo "</tr>";
?>
</tbody>
</table>
</div>
<?PHP
}
?>
由于表在循环中.问题是何时出现第一个表格.并点击第一个标题复选框,它将选中其他表中的所有复选框.我不想发生.我正在寻找的一种方法是,是否有一种方法也可以迭代复选框的ID及其类.或有其他方法可以完成我想发生的事情.
如你看到的.那3张桌子有自己的复选框标题,我想在那里检查所有里面的桌子.你怎么会是聪明的主意.
提前致谢..
解决方法:
您可以这样做.
$('.allcb').on('click', function(){
var childClass = $(this).attr('data-child');
$('.'+childClass+'').prop('checked', this.checked);
});
更新
只需将类allcb应用于主复选框,并将子类应用于名为chk的类.这应该适合您的需求.这是更新的
FIDDLE