选中所有复选框

hai*_*der 3 javascript php checkbox jquery

我有一个带复选框的表,我想要"检查所有"和"取消全部检查"复选框,但我找不到检查所有复选框的方法.

这是我的代码:

<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> 
Run Code Online (Sandbox Code Playgroud)

jen*_*ram 8

$(function() {
    $('.chk_boxes').click(function() {
        $('.chk_boxes1').prop('checked', this.checked);
    });
});
Run Code Online (Sandbox Code Playgroud)

这只会影响check all框.但是为什么你还想要使用两个复选框呢?一个检查所有,一个取消选中所有,但不互斥.那必须是混淆用户的秘诀:)

  • 仅适用于jQuery 1.6.x或更高版本.Alex R将使用*any*版本的jQuery,除了非常短暂的1.6.0,由于这个原因持续了大约两周. (2认同)