检查表中的所有数据值

时间:2015-10-18 10:20:01

标签: javascript php jquery checkbox

我的代码如下:

<div class="input-group select-all" >
    <span class="input-group-addon disabled">
        <input type="checkbox" <?php if($pagetitle=="Trash") echo "disabled"?>>
            </span>
                <div class="input-group-btn">
                    <button type="button" class="btn btn-default dropdown-toggle <?php if($pagetitle=="Trash") echo "disabled"?>" data-toggle="dropdown" tabindex="-1">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">Delete</a></li>
                    </ul>
                </div>
            </div><!-- /input-group -->

当我们签入复选框时,必须选择表格中的所有数据,这是我需要的操作类型。

<div class="mails">
            <table class="table table-hover table-condensed">
                <?php
                if(count($inMessages)>0)
                    foreach($inMessages as $m):
                        ?>
                        <tr data-id="<?php echo $m->sn?>" <?php if($m->flag==1) echo "class=\"read\""?><?php if($m->flag==0) echo "class=\"unread\""?>>
                            <td><?php if($pagetitle!="Trash"):?><i class="fa fa-check-square-o fa-square-o disabled" data-id="<?php echo $m->sn?>"></i><?php endif?></td>
                            <td class="subject"><?php echo $m->subject?></td>
                            <td class="body"><?php echo $m->message?></td>
                            <td>
                                <?php echo $m->address?>
                                <BR/><?php echo $m->io?>
                            </td>
                            <td class="time"><?php echo $m->mdate?> <?php echo $m->mtime?></td>
                        </tr>
                    <?php
                    endforeach
                ?>
            </table>
        </div>
    </div><!-- /Right Side mail bar -->

手动复选框工作正常,但我需要检查所有选项。

除了上面的代码之外,我该怎么做?请帮帮我。

1 个答案:

答案 0 :(得分:0)

如果您想通过一个复选框选择所有选项,则可以使用类似于:

的内容
<?php
$string .= '
<table id="mytable" class="table table-hover table-bordered datatable">
  <thead>
    <tr>
      <th><input type="checkbox" id="selectall"></th>
      <th>Field A</th>
      <th>Field B</th>
    </tr>
  </thead>
  <tbody>'."\n";
foreach($array as $row) { // create table content
  $string .= '    <tr id="art-'.$row['field'].'" class="data">
      <td><input class="case" type="checkbox" name="id[]" value="'.$row['field'].'"></td>
      <td>'.$row['field A'].'</td>
      <td>'.$row['field B'].'</td>
    </tr>'."\n";
}
  $string .= '    </tbody>
</table>
'."\n";
echo $string;
?>
<script>
$(document).ready(function(){
  $("#selectall").click(function () {  // check all
    $('.case').prop('checked', this.checked);
  });

  // if all checkbox are selected, check the selectall checkbox
  // and viceversa
  $(".case").click(function(){
    if($(".case").length == $(".case:checked").length) {
      $("#selectall").prop("checked", "checked");
    } else {
      $("#selectall").removeAttr("checked");
    }
  });
});
</script>