【问题标题】:How to display show / hide table on plus minus button click如何在加减按钮单击时显示显示/隐藏表格
【发布时间】:2020-12-14 20:50:10
【问题描述】:

//JQuery Script one 
 $(document).ready(function(){
            $('.table1').on('click',function(){

                $('.table1shw').show();

            });
            
        });
        
        
//I try second method too but unable to display. 
document.getElementById('sp1').addEventListener("click", function() {
  showTable('t1');
});





function showTable(table) {
  var tables = ['t1'];
  for (var i = 0; i < 4; i++) {
    document.getElementById(tables[i]).style.display = "none";
  }
  document.getElementById(table).style.display = "block";

}
.table-hide{
    display: none;
}
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/fontawesome.css" rel="stylesheet"/>
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="col-md-12 col-12 table-responsive">
                                <div class="form-group">
                                
                                    <div class="col-md-12" style="padding: 10px !important;" align="center">
                                        <div class="form-group">Main heading</div>
                                    </div>

                                    <table class="table table-hover">
                                        <thead class="lbbg3">
                                            <tr>
                                                <td style="width: 800px;">Sub heading 1</td>
                                                <td>Sub heading 2</td>
        
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <th style="background: #eef7fc; text-align: left;" colspan="2"><button class="table-plus-btn table1" id="sp1"><i class="fa fa-plus"></i></div> <button class="table-minus-btn"><i class="fa fa-minus"></i></button> Child Heading</th>

                                            </tr>
                                            <tr>
                                                <td colspan="2">
                                                   <div class="table1shw">  <table class="table1 table-hover table-hide" id="t1">                                    <tr>
                                                            <td style="text-align: left;width: 800px;">
                                                                Row 1
                                                            </td>
                                                            <td style="text-align: center;">
                                                                <div class="custom-control custom-radio radio-table">
                                                                    <a href="#doc"> <input type="radio" id="customRadio50" name="customRadio" class="custom-control-input fcy edu docbtn">
                                                                        <label class="custom-control-label" for="customRadio50" style="cursor: pointer;" onchange="valueChanged()"></label></a>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="text-align: left;width: 800px;">
                                                                Row 2
                                                            </td>
                                                            <td style="text-align: center;">
                                                                <div class="custom-control custom-radio radio-table ">
                                                                    <input type="radio" id="customRadio51" name="customRadio" class="custom-control-input fcy">
                                                                    <label class="custom-control-label" for="customRadio51" style="cursor: pointer;"></label>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    </table></div>
                                                  
                                                </td>
                                            </tr>
                                        </tbody>

                                    </table>
                                </div>
                            </div>

您好,谢谢!一旦我点击加号按钮,我试图显示完整的表格,一旦我点击减号按钮,同一个表格就会隐藏。我会以两种不同的方式完成此操作,但我对此没有任何运气。所以我对如何解决感到沮丧,你能告诉我我做错了什么以及在哪里。请帮忙。

【问题讨论】:

  • 请整理一下你的html代码。

标签: javascript html jquery css bootstrap-4


【解决方案1】:

您没有在正确的元素上调用 .show()

我已经更新了你的代码 -

$(document).ready(function() {
  $("#t1").hide(); // hide table by default
  $('#sp1').on('click', function() {
    $("#t1").show();
  });
  $('#close').on('click', function() {
    $("#t1").hide();
  });
});
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/fontawesome.css" rel="stylesheet" />
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-md-12 col-12 table-responsive">
  <div class="form-group">
    <div class="col-md-12" style="padding: 10px !important;" align="center">
      <div class="form-group">Main heading</div>
    </div>

    <table class="table table-hover">
      <thead class="lbbg3">
        <tr>
          <td style="width: 800px;">Sub heading 1</td>
          <td>Sub heading 2</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th style="background: #eef7fc; text-align: left;" colspan="2">
            <button class="table-plus-btn table1" id="sp1"><i class="fa fa-plus"></i></button><button class="table-minus-btn" id="close"><i class="fa fa-minus"></i></button> Child Heading
          </th>
        </tr>
        <tr>
          <td colspan="2">
            <div class="table1shw">
              <table class="table1 table-hover" id="t1">
                <tr>
                  <td style="text-align: left; width: 800px;">
                    Row 1
                  </td>
                  <td style="text-align: center;">
                    <div class="custom-control custom-radio radio-table">
                      <a href="#doc">
                        <input type="radio" id="customRadio50" name="customRadio" class="custom-control-input fcy edu docbtn" />
                        <label class="custom-control-label" for="customRadio50" style="cursor: pointer;" onchange="valueChanged()"></label>
                      </a>
                    </div>
                  </td>
                </tr>
                <tr>
                  <td style="text-align: left; width: 800px;">
                    Row 2
                  </td>
                  <td style="text-align: center;">
                    <div class="custom-control custom-radio radio-table">
                      <input type="radio" id="customRadio51" name="customRadio" class="custom-control-input fcy" />
                      <label class="custom-control-label" for="customRadio51" style="cursor: pointer;"></label>
                    </div>
                  </td>
                </tr>
              </table>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

我的改变是 -

$("#t1").hide(); - 默认隐藏表格,使用t1,这是表格的id

sp1 上添加点击处理程序,这是加号按钮的 id 并在事件中显示表格 -

$('#sp1').on('click', function() {
     $("#t1").show();
});

最后在其 id close 的关闭按钮上添加点击处理程序

$('#close').on('click', function() {
     $("#t1").hide();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 2013-04-24
    • 1970-01-01
    • 2021-03-19
    • 2021-03-27
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多