gpt4 book ai didi

php - JQuery 选中/取消选中表 php 数组中的所有复选框

转载 作者:行者123 更新时间:2023-12-01 05:42:33 24 4
gpt4 key购买 nike

我使用 php 在数组表 mysql 上显示数据,并使用 jquery 函数选中/取消选中所有复选框。但问题是我使用jQuery函数来检测并执行基于复选框的类的功能。我使用了一个checkbox数组,这样checkbox和类的名称都可以根据mysql中的数据增加。

Jquery 代码:

<SCRIPT language="javascript">
$(document).ready(function() {
$('#selectall').click(function(event) { //on click
if(this.checked) { // check select status
$('.PILIH').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.PILIH').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});


});

</SCRIPT>

PHP 代码:

<a href="add-komitmen.php" style="text-decoration:none;"><input type="button" value="+Add Komitmen"/></a> <input type="submit" value="Hapus" name="HAPUS" />
<!--<input type="checkbox" id="selectall"/>-->
<br>
<div class="scroll">

<?php
$sql2 = "Select master_outlet.ID_OUTLET, master_outlet.NAMA_OUTLET, tb_komitmen.ID_OUTLET
from master_outlet, tb_komitmen
where master_outlet.ID_OUTLET = tb_komitmen.ID_OUTLET group by master_outlet.ID_OUTLET";

$hasil2 = mysqli_query($mysqli, $sql2);

while($data2 = mysqli_fetch_array($hasil2)) {
$yoo = $data2[ID_OUTLET];
$outlet = $data2[NAMA_OUTLET];

//echo $yoo;

echo "<table border='1' cellspacing='0' width='100%'>
<thead>
<tr><th width='9%'>Check <input type='checkbox' id='selectall'/></th>
<th width='5%'>No.</th>

<th>$outlet</th>
<th width='17%'>Komitmen</th>
<!-- <th>Date</th>-->
<th width='10%'>Detail</th>

</tr>
</thead>
<tbody>";

$sql = "Select AA.* from(SELECT tb_komitmen.ID_KOMITMEN AS ID,
master_outlet.NAMA_OUTLET AS NAMA_OUTLET,
master_produk.NAMA_PRODUK AS NAMA_PRODUK
,tb_komitmen.KOMITMEN AS KOMITMEN, master_outlet.ID_OUTLET AS ID_OUTLET

FROM tb_komitmen, master_outlet, master_produk
WHERE master_outlet.ID_OUTLET = tb_komitmen.ID_OUTLET
AND master_produk.ID_PRODUK = tb_komitmen.ID_PRODUK) as AA where ID_OUTLET = '$yoo'";

$hasil = mysqli_query($mysqli, $sql);
$num_results = mysqli_num_rows($shasil);
if($hasil)
{
if(mysqli_num_rows($hasil)!=0) {

$no = 1;
while($data = mysqli_fetch_array($hasil)) {
//$min = $data[KOMITMEN];

echo "<tr>";
echo "<td align='center'><input type='checkbox' value='$data[ID]' name='PILIH[]' class='PILIH' ></td>";
echo "<td align='center'>$no</td>";
//echo "<td align='center'>$data[NAMA_OUTLET]</td>";
echo "<td align='center'>$data[NAMA_PRODUK]</td>";
echo "<td align='center'>$data[KOMITMEN]</td>";
//echo "<td align='center'>$data[TGL]</td>";
echo "<td align='center'><a href='detail-stc-otl-produk.php?ID=$data[ID]' style='text-decoration:none;'><input type='button' value='detail'></a></td>";
echo "</tr> ";
$no++;
}
}
echo "<br> ";
}
}

echo " </tbody>
</table>" ;
?>

</div>

</form>

上述代码的概述。如果我单击“全部选中”,那么所有复选框都会全部选中。我希望如果我单击“检查”,则第一个表中的所有复选框都会被选中。如果我单击 2,则仅选中表 2 中的所有仅检查复选框。

! an overview of the above code.if i click check All then all the checkboxes will check all.I wish if I click check all the checkboxes in the first table are checks.and if I click 2 then check all the checks only checkbox in table 2 only.

最佳答案

试试这个:

$(document).ready(function() {
$('#selectall').click(function(event) { //on click
if(this.checked) { // check select status
$(this).parent("table").find("input[type=checkbox]").attr("checked",true);
}else{
$(this).parent("table").find("input[type=checkbox]").attr("checked",false);

}
});


});

关于php - JQuery 选中/取消选中表 php 数组中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29887525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com