douzhi3105 2019-07-06 13:07
浏览 346
已采纳

如何使用所选复选框隐藏和显示表列

I have a table and in that table I give check box in <th> like <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="1" />Sr No</th>. Currently it gives me the value of selected columns. Now I want to show and hide this column.

Following are my code

<form role="form" id="print_loading_sheet" method="POST" enctype="multipart/form-data">

<section class="content">
<div class="row">
 <div class="table-responsive">
   <table id="delivery_checklist_table" class="table table-bordered  table-sm" style=" overflow: auto;">

 <thead>
  <tr>
    <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="1" />Sr No</th>
    <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="2" />Bilty Id</th>
    <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="3" />LR No</th>
    <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="4" />Consignor Name</th>
    <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="5" />Consignor GSTIN</th>
    <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="6" />Consignee Name</th>
    <th><input type="checkbox" id="selectedrecord" name="selectedrecord" value="7" />Consignee GSTIN</th>
 </tr>

  </thead>
  <tbody>

    <?php $counter = 1; ?> 
    <?php foreach($bilties as $bilty):?>

  <tr>
  <td><?php echo $counter;?></td>
  <td><?php echo $bilty->id;?></td>
  <td><?php echo $bilty->lr_no;?></td>
  <td><?php echo $bilty->consignor;?></td>
  <td><?php echo $bilty->consignor_gst_no;?></td>
  <td><?php echo $bilty->consignee;?></td>
  <td><?php echo $bilty->consignee_gst_no;?></td>
 </tr>
     <?php $counter++; ?>
     <?php endforeach; ?>              
</tbody> 
    </table>
   </div>
 </div>

    </section>
      <button id="print" name="print" class="btn btn-block btn-outline-primary fa fa-newspaper-o col-10 offset-1" style="margin-top: 35px; margin-bottom: 25px;" data-clicked="unclicked"> Print </<button type=""></button>> 
  </form>
  </div>

<script>
  $('#print').click(function (event) {
    event.preventDefault();

  var allVals = [];

  $('input[name=selectedrecord]:checked').each(function() {
    allVals.push($(this).val());
  });
    console.log("check Column"+ allVals);
    alert(allVals);
});
</script>

</body> 
</html>  
  • 写回答

4条回答 默认 最新

  • douwei1930 2019-07-06 14:27
    关注

    You can iterate over the array to set the display property with eq():

    allVals.forEach(function(i){
      $('tr').each(function(){
        $(this).find('td, th').eq(i-1).css('display', 'none');
      });
    });
    

    Demo:

    $('#print').click(function (event) {
      $('td').css('visibility', 'visible');
      event.preventDefault();
      var allVals = [];
      $('input[name=selectedrecord]:checked').each(function() {
        allVals.push($(this).val());
      });
      //console.log("check Column"+ allVals);
      allVals.forEach(function(i){
        $('tr').each(function(){
          $(this).find('td, th').eq(i-1).css('display', 'none');
        });
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form role="form" id="print_loading_sheet" method="POST" enctype="multipart/form-data">
    
      <section class="content">
        <div class="row">
          <div class="table-responsive">
            <table id="delivery_checklist_table" class="table table-bordered  table-sm" style=" overflow: auto;">
              <thead>
              <tr>
                <th><input type="checkbox" name="selectedrecord" value="1" />Sr No</th>
                <th><input type="checkbox" name="selectedrecord" value="2" />Bilty Id</th>
                <th><input type="checkbox" name="selectedrecord" value="3" />LR No</th>
              </tr>
    
              </thead>
              <tbody>
                <tr>
                  <td>1xy</td>
                  <td>1abc</td>
                  <td>1mnl</td>
                </tr> 
                 <tr>
                  <td>2xy</td>
                  <td>2abc</td>
                  <td>2mnl</td>
                </tr> 
              </tbody> 
            </table>
          </div>
        </div>
    
      </section>
      <button id="print" name="print" class="btn btn-block btn-outline-primary fa fa-newspaper-o col-10 offset-1" style="margin-top: 35px; margin-bottom: 25px;" data-clicked="unclicked"> Print</button>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备