【问题标题】:Toggle Plus / Minus single Button to show and hide table切换加/减单个按钮以显示和隐藏表格
【发布时间】:2021-03-27 08:11:50
【问题描述】:

$(document).ready(function() {
  $("#t1").hide(); // hide table by default
  $('#sp1').on('click', function() {
    $("#t1").show();
  });
  $('#close').on('click', function() {
    $("#t1").hide();
  });
});
<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.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet"/>
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/fontawesome.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>

嗨,目前我正在使用两个按钮加号和减号,只是我需要一次显示一个按钮。当加号按钮单击隐藏表将显示,减号按钮应该出现,加号按钮是隐藏的。减号按钮也一样。更多我们可以说切换按钮。也需要我们可以一次只显示一个隐藏的表格。

目前所有代码和表格都可以正常工作,我将这种方法用于手风琴,但不用于表格。

【问题讨论】:

    标签: javascript html css bootstrap-4


    【解决方案1】:

    如果我没记错的话,这就是你想要做的: 首先 - 显示加号按钮,并隐藏表格 第二 - 单击加号按钮时显示表格,隐藏加号按钮,并显示减号按钮 第三-当点击减号按钮时,隐藏减号按钮,显示加号按钮,隐藏表格。

    您的代码似乎无法正常工作,因为您没有在适当的时间隐藏/显示按钮。我还假设一次应该只显示一个按钮?

    $(document).ready(function() {
            $("#t1").hide(); // hide table by default
            $("#close").hide(); //hide the minus button as well if you only want one button to display at a time
    
      $('#sp1').on('click', function() { //when plus button is clicked
        $("#t1").show(); 
        $("#sp1").hide(); //you need to hide the plus button now
        $("#close").show(); //and then display the minus button
      });
      $('#close').on('click', function() { 
        $("#t1").hide(); //hide table
        $("#close").hide(); //hide minus btn
        $("#sp1").show(); //show plus button
      });
    });
    

    【讨论】:

    • 假设如果将它用于两行并且我不想打开的行此时只有一行会打开,第二行会关闭。我该怎么办。
    • 您的代码运行良好,感谢您的帮助
    • 您也可以这样做,显示一个表格,然后在单击加号按钮时显示另一个表格。单击减号按钮时,隐藏第二个表格。
    • 这里就像这个脚本一样使用,但第一个表仍然打开,直到我不会点击减号。一旦点击第二个加号按钮,我需要上表消失
    • 所以你希望两个表都被隐藏,当点击加号时应该显示一个表,当再次点击加号时应该显示另一个表?
    【解决方案2】:

    你也可以写得短一点

    $(document).ready(function() {
      $("#t1, #close").hide();
      $('#sp1, #close').on('click', function() {
        $('#t1, .table-plus-btn, .table-minus-btn').toggle();
      });
    });
    

    【讨论】:

      【解决方案3】:

      您可以根据点击隐藏/显示按钮。如果要显示或隐藏行,我添加了函数 toggleButtons(show) ,其中 show 等于。

      function toggleButtons(show) {
            if (show) {
              $("#sp1").hide();
              $("#close").show();
            } else {
              $("#sp1").show();
              $("#close").hide();
            }
        }
      

      $(document).ready(function() {
        $("#t1").hide(); // hide table by default
        $('#sp1').on('click', function() {
          toggleButtons(true)
          $("#t1").show();
        });
        $('#close').on('click', function() {
          toggleButtons(false)
          $("#t1").hide();
        });
        
        
        function toggleButtons(show) {
            if (show) {
              $("#sp1").hide();
              $("#close").show();
            } else {
              $("#sp1").show();
              $("#close").hide();
            }
        }
      });
      <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.3.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
      <link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet"/>
      <link href="https://pro.fontawesome.com/releases/v5.10.0/css/fontawesome.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" style="display: none"><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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 2020-07-05
        • 2019-09-28
        相关资源
        最近更新 更多