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

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3