2016-11-23 85 views
0

我有一个复选框的问题。我想检查所有当第一个复选框被选中。这是我的代码所有勾选第一个复选框时选中

<tr> 
    <td><label for="">Pilih Pendidikan</label></td> 
    <td style="text-align:left;"> 
    <input type="checkbox" id="pilih_semua_pdk">Pilih Semua 
    <?php foreach ($query2 as $row2) {?> 
     <input class="form-control required check_pdk" type="checkbox" name="pendidikan[]" value="<?php echo $row2[id_pendidikan]; ?>" 
      <?php 
      $pendidikan = $_POST[pendidikan]; 
       if ($pendidikan !=NULL) { 
        foreach($pendidikan as $pendidikan){ 
        if ($pendidikan == $row2[id_pendidikan]) { 
         echo "checked"; 
        } 
       } 
       } 
      ?> 
     ><?php echo $row2[nama_pendidikan]; ?> 
    <?php } ?> 
    </td> 
</tr> 

我试试这个代码,但不工作

<script> 
    $("#pilih_semua_pdk").click(function() { 
     $(".check_pdk").prop('checked', $(this).prop('checked')); 
</script> 
+4

份额呈现的HTML,而不是PHP代码 –

+1

有带班'checkBoxClass' –

+0

没有元素,其中,与id元素'ckbCheckAll' –

回答

0

jQuery使这一块蛋糕。

$(document).ready(function(){ 
    // DOM has been loaded, attach click event for first checkbox 

    $('#pilih_semua_pdk').on('click', function(){ 
     // verify if it is checked 
     if($(this).is(':checked')) 
     { 
      // check/tick the remaining checkboxes of the same td 
      $(this).siblings('.check_pdk').prop('checked', 'checked'); 
     } 
     else 
     { 
      // do something when the first checkbox is unchecked 
     } 
    }); 
}); 
1

试试这个,你需要一个click事件上的第一个复选框绑定。点击第一个复选框,检查是否检查!如果是,那么找到所有复选框即可。

$('#pilih_semua_pdk').on('click', function(){ 
    if($(this).is(':checked')) 
    { 
     //find parent td and its all checkboxes excluding first 
     $(this).parent().find('input[type=checkbox]').not('#pilih_semua_pdk').each(function(){ 
      $(this).prop('checked', true); 
     }); 
    } 
}); 
相关问题