Php 选中所有复选框

Php 选中所有复选框,php,javascript,jquery,checkbox,Php,Javascript,Jquery,Checkbox,我有一个带有复选框的表,我想执行“全部选中”和“取消全部选中”复选框,但我找不到选中所有复选框的方法 这是我的密码: <form> <?php foreach ($this->entries as $entry): ?> <tr class="table_head_seperator"> <td class="grid_info" width="32px" bgcolor="#f6f6f6">&

我有一个带有复选框的表,我想执行“全部选中”和“取消全部选中”复选框,但我找不到选中所有复选框的方法

这是我的密码:

<form>
    <?php foreach ($this->entries as $entry): ?>
        <tr class="table_head_seperator">
            <td class="grid_info" width="32px" bgcolor="#f6f6f6"><input type="checkbox" class="chk_boxes1" name="to_delete[<?PHP echo $entry['id'] ?>]"  /></td>
            <td class="grid_info" width="160px" bgcolor="#eeeeee"><span class="country_name"><?php echo $entry['user_name'] ?></span></td>
            <td class="grid_info" width="130px" bgcolor="#eeeeee"><span class="country_name"><?php echo $entry['date_created'] ?></span></td>
            <td class="grid_info" width="100px" bgcolor="#f6f6f6"><span class="country_name"><?php echo $entry['user_type_name'] ?></span></td>
        </tr>
    <?PHP endforeach; ?>

    <input type="checkbox" class="chk_boxes" label="check all"  />check all
    <input type="checkbox" class="unchk_boxes"   /> un-check all
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $('.chk_boxes').click(function(){
            $('.chk_boxes1').attr('checked',checked)
        })
    });
</script> 

我认为您应该对复选框属性使用true或false。

您忘记了引号:

$('.chk_boxes1').attr('checked','checked')
小提琴:

试着用真或假。像这样:

$('.chk_boxes1').attr('checked',true);
补充意见:

此外,您的“取消选中”和“选中所有”复选框似乎是多余的

<input type="checkbox" class="chk_boxes" label="check all"  />check all
<input type="checkbox" class="unchk_boxes"   /> un-check all
使用HTML:

<input type="checkbox" class="chk_boxes" label="check all"  />check all
这只会影响“全部选中”框。但你为什么要使用两个复选框呢?一个选中全部,一个取消选中全部,但不是相互排斥的。这一定是让用户困惑的秘诀:

试试这个

 $(".chk_boxes").click(function()
   {
   var checked_status = this.checked;
   $(".chk_boxes1").each(function()
     {
      this.checked = checked_status;
     });
  });

因此,如果在使用类chk_box选中或取消选中复选框时要影响的所有复选框的类名为chk_boxes1,则将获取所有元素并相应地设置checked属性

请参见此处的工作解决方案

<script type="text/javascript" charset="utf-8">
$('document').ready( function() {
    $('.chk_boxes').click( function() {
        $(':checkbox').attr('checked',true);
    });
});
</script>
您不需要使用“全部取消选中”复选框:

$('.chk_boxes').click(function(){
    var chk = $(this).attr('checked')?true:false;
    $('.chk_boxes1').attr('checked',chk);
});

关于全部选中/全部取消选中的建议。。 如我所想,在选择全选后,如果用户单击任何复选框1,则应取消选中全选复选框,也不必单击全选复选框,如果用户逐个选择所有复选框1,则应选中复选框。因此,我建议您在使用check all/uncheck all时尝试此功能

$(document).ready(function() {
    $('.chk_boxes').click(function(){
        $('.chk_boxes1').attr('checked',$(this).attr('checked'));
    })
    $('.chk_boxes1').click(function(){
        if($('.chk_boxes1').length == $('.chk_boxes1:checked').length)
              $('.chk_boxes').attr('checked',checked)
        else
             $('.chk_boxes').attr('checked',false)
    })
});

选中取消选中全部/选择取消选中所有复选框 遵循下面的代码,下面的代码非常简单

在html表单中添加以下代码行

<input type="checkbox" id='checkall' /> Select All
然后添加以下脚本

     <script type='text/javascript'>
 $(document).ready(function(){
   // Check or Uncheck All checkboxes
   $("#checkall").change(function(){
     var checked = $(this).is(':checked');
     if(checked){
       $(".checkbox").each(function(){
         $(this).prop("checked",true);
       });
     }else{
       $(".checkbox").each(function(){
         $(this).prop("checked",false);
       });
     }
   });
 
  // Changing state of CheckAll checkbox 
  $(".checkbox").click(function(){
 
    if($(".checkbox").length == $(".checkbox:checked").length) {
      $("#checkall").prop("checked", true);
    } else {
      $("#checkall").removeAttr("checked");
    }

  });
});
</script>
请记住,脚本代码中的.checkbox是html表单的其他复选框中的类名

例如,如果您有如下输入复选框

<input class="checkbox" name="emailid[]" type="checkbox" value="<?php echo $email;?>" />

仅适用于jQuery 1.6.x或更高版本。Alex R将与任何版本的jQuery一起使用,除了非常短暂的1.6.0版本,由于这个原因,它持续了大约两周。@T.J.Crowder的确,如果在1.6.0以下运行jQuery,则.prop可以替换为.attr。感谢您的澄清。我的观点是attr也适用于1.6.1及以上版本。因此,如果要使用jQuery访问器来实现这一点,我会使用attr而不是prop。但马匹的课程,并在一年或两年内,每个人都将在1.6无论如何-请,谢谢+1它与[php]标记有什么关系?
     <script type='text/javascript'>
 $(document).ready(function(){
   // Check or Uncheck All checkboxes
   $("#checkall").change(function(){
     var checked = $(this).is(':checked');
     if(checked){
       $(".checkbox").each(function(){
         $(this).prop("checked",true);
       });
     }else{
       $(".checkbox").each(function(){
         $(this).prop("checked",false);
       });
     }
   });
 
  // Changing state of CheckAll checkbox 
  $(".checkbox").click(function(){
 
    if($(".checkbox").length == $(".checkbox:checked").length) {
      $("#checkall").prop("checked", true);
    } else {
      $("#checkall").removeAttr("checked");
    }

  });
});
</script>
<input class="checkbox" name="emailid[]" type="checkbox" value="<?php echo $email;?>" />