【问题标题】:How to open a div based on checkbox switch click?如何根据复选框开关点击打开一个div?
【发布时间】:2020-04-12 08:26:15
【问题描述】:

我创建了一个引导开关,并且我在开关底部有一个 div,所以当开关打开时我希望 div 显示,当它关闭时我希望 div 隐藏。我找到了一些只显示复选框的解决方案。

我该怎么做?

例如,如果开关复选框被选中,那么我需要显示 div 的内容,否则隐藏 div。

.tags-list-group {
    margin-bottom: 50px;
}

.tags-list-group .switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
    float: right;
}

.tags-list-group .switch input {
    display: none;
}

.tags-list-group .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    -webkit-transition: .4s;
    transition: .4s;
}

.tags-list-group .slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
}

.tags-list-group input:checked + .slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
}

.tags-list-group .slider.round {
    border-radius: 34px;
}

.tags-list-group .slider.round:before {
    border-radius: 50%;
}

.tags-list-group input.success:checked + .slider {
    background-color: #8bc34a;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/js/bootstrap.min.js"></script>

<div class="tags-list-group">
    <div class="tags-group-card">
        <!-- Default panel contents -->
        <ul class="list-group list-group-flush">
            <li class="list-group-item custom-list-group-item">
                User Defined
                <label class="switch ">
                    <input type="checkbox" class="success">
                    <span class="slider round"></span>
                </label>
            </li>
        </ul>
    </div>
    <div class="list-group-item show-content">
        <p>Content 1(hide div)</p>
    </div>

</div>

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    这对于 jquery 来说非常简单。查看以下示例

    $(document).ready(() => {
        $("#content").hide();
    
        $("#chkbox").click((e) => {
            if ($("#chkbox").is(":checked")) {
                $("#content").show();
            } else {
                $("#content").hide();
            }
        });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <input type="checkbox" name="" id="chkbox" />
    <div id="content">
        <p>Hidden Content</p>
    </div>

    【讨论】:

    • 答案在正确的轨道上,尽量使用问题中提出的原厂组件
    【解决方案2】:

    您可以将.click() 附加到滑块上,然后使用.toggle() 显示/隐藏元素,这是一个有效的sn-p:

    $(document).ready(function(){
      $('.show-content').hide();
      $('.slider').click(function(e) {
        $('.show-content').toggle();
      })
    })
    .tags-list-group {
        margin-bottom: 50px;
    }
    
    .tags-list-group .switch {
        position: relative;
        display: inline-block;
        width: 60px;
        height: 34px;
        float: right;
    }
    
    .tags-list-group .switch input {
        display: none;
    }
    
    .tags-list-group .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #ccc;
        -webkit-transition: .4s;
        transition: .4s;
    }
    
    .tags-list-group .slider:before {
        position: absolute;
        content: "";
        height: 26px;
        width: 26px;
        left: 4px;
        bottom: 4px;
        background-color: white;
        -webkit-transition: .4s;
        transition: .4s;
    }
    
    .tags-list-group input:checked + .slider:before {
        -webkit-transform: translateX(26px);
        -ms-transform: translateX(26px);
        transform: translateX(26px);
    }
    
    .tags-list-group .slider.round {
        border-radius: 34px;
    }
    
    .tags-list-group .slider.round:before {
        border-radius: 50%;
    }
    
    .tags-list-group input.success:checked + .slider {
        background-color: #8bc34a;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/js/bootstrap.min.js"></script>
    
    <div class="tags-list-group">
        <div class="tags-group-card">
            <!-- Default panel contents -->
            <ul class="list-group list-group-flush">
                <li class="list-group-item custom-list-group-item">
                    User Defined
                    <label class="switch ">
                        <input type="checkbox" class="success">
                        <span class="slider round"></span>
                    </label>
                </li>
            </ul>
        </div>
        <div class="list-group-item show-content">
            <p>Content 1(hide div)</p>
        </div>
    
    </div>

    【讨论】:

    • is on I want the div to show and when it's off I want the div to hide 反其道而行之
    • 在运行前使用hide() 修复它。
    【解决方案3】:

    使用 JQuery 你可以实现这样的目标:

    $('.show-content').hide() // Init with hidden .show-content
    
    $('input.success').change(function() {
      if (this.checked) {
        $('.show-content').show()
      } else {
        $('.show-content').hide()
      }
    });
    .tags-list-group {
      margin-bottom: 50px;
    }
    
    .tags-list-group .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
      float: right;
    }
    
    .tags-list-group .switch input {
      display: none;
    }
    
    .tags-list-group .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .tags-list-group .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .tags-list-group input:checked+.slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    .tags-list-group .slider.round {
      border-radius: 34px;
    }
    
    .tags-list-group .slider.round:before {
      border-radius: 50%;
    }
    
    .tags-list-group input.success:checked+.slider {
      background-color: #8bc34a;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/js/bootstrap.bundle.min.js"></script>
    
    <div class="tags-list-group">
      <div class="tags-group-card">
        <!-- Default panel contents -->
        <ul class="list-group list-group-flush">
          <li class="list-group-item custom-list-group-item">
            User Defined
            <label class="switch ">
              <input type="checkbox" class="success">
              <span class="slider round"></span>
            </label>
          </li>
        </ul>
      </div>
      <div class="list-group-item show-content">
        <p>Content 1(hide div)</p>
      </div>
    
    </div>

    它正在使用函数.show().hide()change 事件。

    【讨论】:

      【解决方案4】:

      这是您想要的,基于您的代码。但是,在我看来,您可以通过使用 Bootstrap 的collapse component 并将您的开关嵌入其中来获得一个更整洁的。

      $("#myCustomCheckbox").change(function(){
      
        // in case the switch is on
        if(this.checked) {
              $("#myDivOfContent").addClass("content-area--show");
              
        // in case switch is off
          }else{
              $("#myDivOfContent").removeClass("content-area--show");
          }
      });
      /*** begin::div of content styles ****/
      
      .content-area {
        height: 0;
        width: 100%;
        overflow: hidden;
        transition: height .3s ease-in-out;
       }
       
       .content-area p {
        padding: 20px;
      }
       
      .content-area--show {
        height: 70px;
        border: 1px solid #ccc;
        border-top: 0;
      }
      
      .switch {
       margin-bottom: 0;
       }
      /*** end::div of content styles ****/
      
      .tags-list-group {
          margin-bottom: 50px;
      }
      
      .tags-list-group .switch {
          position: relative;
          display: inline-block;
          width: 60px;
          height: 34px;
          float: right;
      }
      
      .tags-list-group .switch input {
          display: none;
      }
      
      .tags-list-group .slider {
          position: absolute;
          cursor: pointer;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: #ccc;
          -webkit-transition: .4s;
          transition: .4s;
      }
      
      .tags-list-group .slider:before {
          position: absolute;
          content: "";
          height: 26px;
          width: 26px;
          left: 4px;
          bottom: 4px;
          background-color: white;
          -webkit-transition: .4s;
          transition: .4s;
      }
      
      .tags-list-group input:checked + .slider:before {
          -webkit-transform: translateX(26px);
          -ms-transform: translateX(26px);
          transform: translateX(26px);
      }
      
      .tags-list-group .slider.round {
          border-radius: 34px;
      }
      
      .tags-list-group .slider.round:before {
          border-radius: 50%;
      }
      
      .tags-list-group input.success:checked + .slider {
          background-color: #8bc34a;
      }
      <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" rel="stylesheet"/>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/js/bootstrap.min.js"></script>
      
      <div class="tags-list-group">
          <div class="tags-group-card">
              <!-- Default panel contents -->
              <ul class="list-group list-group-flush">
                  <li class="list-group-item custom-list-group-item ">
                      User Defined
                      <label class="switch">
                          <input type="checkbox" class="success" id="myCustomCheckbox">
                          <span class="slider round"></span>
                      </label>
                  </li>
              </ul>
          </div>
          <div class="content-area" id="myDivOfContent">
              <p>Content 1(hide div)</p>
          </div>
      
      </div>

      【讨论】:

        【解决方案5】:

        使用prop 来检查this

        $(function() {
          $('.success').change(function() {
            if ($(this).prop('checked')) {
              $(".show-content").show();
            } else {
              $(".show-content").hide();
            }
          })
        });
        .tags-list-group {
          margin-bottom: 50px;
        }
        
        .tags-list-group .switch {
          position: relative;
          display: inline-block;
          width: 60px;
          height: 34px;
          float: right;
        }
        
        .tags-list-group .switch input {
          display: none;
        }
        
        .tags-list-group .slider {
          position: absolute;
          cursor: pointer;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: #ccc;
          -webkit-transition: .4s;
          transition: .4s;
        }
        
        .tags-list-group .slider:before {
          position: absolute;
          content: "";
          height: 26px;
          width: 26px;
          left: 4px;
          bottom: 4px;
          background-color: white;
          -webkit-transition: .4s;
          transition: .4s;
        }
        
        .tags-list-group input:checked+.slider:before {
          -webkit-transform: translateX(26px);
          -ms-transform: translateX(26px);
          transform: translateX(26px);
        }
        
        .tags-list-group .slider.round {
          border-radius: 34px;
        }
        
        .tags-list-group .slider.round:before {
          border-radius: 50%;
        }
        
        .tags-list-group input.success:checked+.slider {
          background-color: #8bc34a;
        }
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" rel="stylesheet" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
        <div class="tags-list-group">
          <div class="tags-group-card">
            <!-- Default panel contents -->
            <ul class="list-group list-group-flush">
              <li class="list-group-item custom-list-group-item">
                User Defined
                <label class="switch ">
                            <input type="checkbox" class="success">
                            <span class="slider round"></span>
                        </label>
              </li>
            </ul>
          </div>
          <div class="list-group-item show-content" style="display:none;">
            <p>Content 1(hide div)</p>
          </div>
        
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-09
          相关资源
          最近更新 更多