2015-10-18 47 views
1

我的代碼如下:檢查所有數據值在表

<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 --> 

手動複選框工作正常,但我需要檢查所有選項。

除了上面的代碼,我該怎麼做?請幫幫我。

+0

嗨薩賓,基本上每個錶行似乎有一個數據ID。我是否有權假設你想要每個表格數據項的內容而不是html?這就是你所說的「表中的所有數據」?另外,你如何將這些數據提交回服務器?它看起來像JavaScript可以很容易地完成這項工作,jQuery使這個更簡單的開始。你是否期待有人爲你編寫整個劇本,或者你是否已經開始編寫劇本?一個codepen或jsfiddle或類似的將幫助人們幫助你 – brianlmerritt

回答

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>