使用Bootstrap 4进行​​单选按钮切换扩展

时间:2018-01-21 16:44:36

标签: toggle bootstrap-4

我正在尝试使用Bootstrap 4创建一个表单,在单击每个“按钮”时展开不同的元素。

它应该如何运作: 按钮(收音机):是/否--- 如果单击“是”,则会展开包含复选框按钮的元素。

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary active" data-toggle="collapse" href="#collapseExample" aria-expanded="false"> 
    <input type="radio" name="options" id="option2" autocomplete="off"> Yes
  </label>
  <label class="btn btn-primary">
    <input type="radio" name="options" id="option3" autocomplete="off"> No
  </label>
</div>
<!-- Expanded Buttons -->
<div class="btn-group collapse" id="collapseExample" data-toggle="buttons">
  <label class="btn btn-primary active">
    <input type="checkbox" autocomplete="off"> Checkbox 2
  </label>
  <label class="btn btn-primary">
    <input type="checkbox" autocomplete="off"> Checkbox 3
  </label>
</div>
<!-- / Expand Buttons -->

这是我的笔a link

2 个答案:

答案 0 :(得分:5)

这是一个简单的解决方案,当选中“是”时显示div,如果选中“否”则将其隐藏:

<label>
    <input type="radio" name="radio" data-toggle="collapse" data-target="#collapseExample:not(.show)"> Yes
</label>
<label>
    <input type="radio" name="radio" data-toggle="collapse" data-target="#collapseExample.show"> No
</label>

<div class="collapse" id="collapseExample">
    Gets toggled by radios
</div>

FIDDLE

答案 1 :(得分:1)

如果你只想使用本机Bootstrap 4类(没有任何自定义css),那么这将是一种方法:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div>
    <button class="navbar-toggler pl-1" type="button" data-toggle="collapse" data-target="#collapseExample" aria-controls="collapseExample" aria-expanded="false" aria-label="Toggle navigation">
        <div class="form-check">
            <label class="btn btn-primary active" data-toggle="collapse" data-target="#collapseExample" href="#collapseExample" aria-expanded="false"> 
                <input type="radio" name="options" id="option2" autocomplete="off"> Yes
            </label>
        </div>
    </button>
    <label class="btn btn-primary">
        <input type="radio" name="options" id="option3" autocomplete="off"> No
    </label>

    <div class="collapse pl-4" id="collapseExample">
        <!-- Expanded Buttons -->
        <div class="btn-group collapse" id="collapseExample">
            <label class="btn btn-primary active">
                <input type="checkbox" autocomplete="off"> Checkbox 2
            </label>
            <label class="btn btn-primary">
                <input type="checkbox" autocomplete="off"> Checkbox 3
            </label>
        </div>
        <!-- / Expand Buttons -->
    </div>
</div>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>