简体   繁体   English

jQuery来检查/取消选中表php数组中的所有复选框

[英]JQuery to check/uncheck all checkboxes within tables php array

I display data on the array table mysql with php and check/uncheck all the checkbox with jquery functions. 我用php在数组表mysql上显示数据,并使用jquery函数选中/取消选中所有复选框。 but the problem is that I use jQuery functions to detect and perform the function of a class based on the checkbox. 但是问题是我使用jQuery函数来基于复选框检测并执行类的函数。 I use a checkbox array so chackbox and also the name of the class can be increased according to the data in mysql. 我使用复选框数组,因此可以根据mysql中的数据增加chackbox以及类的名称。

Jquery Code : 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 Code : 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>

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. 我希望如果单击“检查”,则第一张表中的所有复选框均为“ checks”。 and if I click 2 then check all the checks only checkbox in table 2 only. 如果我单击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. 我希望如果单击“检查”,则第一张表中的所有复选框均为“ checks”。 and if I click 2 then check all the checks only checkbox in table 2 only. 如果我单击2,则仅选中表2中的所有仅检查复选框。

Try this: 尝试这个:

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

        }
    });


});

Change this code : 更改此代码:

<input type='checkbox' id='selectall'/>

Into(no need for increment id) 进入(不需要增量ID)

<input type='checkbox' class='selectall'/>

JS JS

$(document).ready(function() { 
   $(document).on('click','.selectall', function(event) {  // better use event delegation if you add the data dynamically

    if(this.checked) { 
        $(this).parent('table').find('.PILIH').prop('checked', true);            
    }else{
        $(this).parent('table').find('.PILIH').prop('checked', false);       
    }
   });

});
$('#allCheck').click(function(event) {
          if (this.checked) {
              $('.chk').each(function() {
                  this.checked=true;
              });
          } else {
              $('.chk').each(function(){
                  this.checked=false;
              });
          }
      });

place id checkall on that checkbox from which you want to check all the checkboxes. 将id checkall放在您要从中检查所有复选框的复选框上。 on which checkbox you want to check on click apply chk class. 您要在哪个复选框上单击,单击Apply chk class。 And write above code in javascript tags. 并在javascript标签中编写以上代码。 Sometimes jQuery tags not work. 有时jQuery标签不起作用。 Then you have need to use document.ready function/you can write js code at the end of body. 然后,您需要使用document.ready函数/可以在正文末尾编写js代码。 In this way your page load speed will be fast. 这样,您的页面加载速度将很快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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