单击选择元素时Bootstrap4下拉菜单自动折叠

时间:2018-04-13 07:27:32

标签: javascript drop-down-menu bootstrap-4 dropdown popper.js

这是我在previous question之后遇到的一个扩展问题。

问题在于当有人点击箭头显示高级搜索时,它会按预期下拉表单。但是,当有人点击选择下拉菜单时,整个下拉菜单就会自动崩溃。

我一直试图找出导致此问题的元素/触发器,看看是否有办法解决这个问题,但我根本没有进展

这是我到目前为止所观察到的

  1. [预期]如果用户在下拉菜单区域外单击,则菜单会崩溃
  2. [意外]如果用户点击了下拉菜单中的任何空白 ,菜单仍会崩溃
  3. [预期]如果用户点击了文本字段,复选框,radiobox,textarea,则他们应该能够输入并输入所需的文本。
  4. [意外]如果用户点击了选择菜单,则dropmenu会立即崩溃
  5. 我已经删除了下面的示例代码来演示我的问题。

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <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>
        <div class="container">
          <div class="row">
            <div class="col-12">
              <div class="input-group dropdown">
                <input type="text" class="form-control" id="project_search" placeholder="search ...">
                <div class="input-group-append">
                  <div class="btn-group">
                    <button class="btn bg-primary text-white" type="button">Search</button>
                    <button class="btn bg-primary text-white dropdown-toggle dropdown-toggle-split" id="searchAdvancedFilter" data-toggle="dropdown" data-boundary="window" aria-haspopup="true" aria-expanded="false">
                      <span class="caret"></span>
                      <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <div class="dropdown-menu dropdown-menu-right p-3 w-100" aria-labelledby="searchAdvancedFilter">
                      <h6 class="dropdown-header">Advanced Settings</h6>
                      <div class="dropdown-divider"></div>
                      <div class="form-group">
                        <label for="option">Option Header</label>
                        <select class="form-control" id="option" name="option">
                          <option>Pick an option</option>
                          <option value=1>Option 1</option>
                          <option value=2>Option 2</option>
                        </select>
                      </div>
                      <div class="form-group">
                        <label for="inputtext">Input Header</label>
                        <input type="text" class="form-control" id="inputtext" name="inputtext">
                      </div>
                      <div class="form-group">
                        <label for="inputcb">Checkbox Header</label>
                        <input type="checkbox" class="form-control" id="inputcb" name="inputcb">
                      </div>
                      <div class="form-group">
                        <label for="inputradio">Radio Header</label>
                        <input type="radio" class="form-control" id="inputradio" name="inputradio">
                      </div>
                      <div class="form-group">
                        <label for="ta">TextArea Header</label>
                        <textarea class="form-control" id="ta" name="ta"></textarea>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

1 个答案:

答案 0 :(得分:3)

当使用<form>时,引导下拉菜单将始终在点击时关闭,外。防止它在点击时关闭的方法是在下拉列表中使用<form>元素...

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<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>

<div class="container">
<div class="row">
    <div class="col-12">
        <div class="input-group dropdown">
            <input type="text" class="form-control" id="project_search" placeholder="search ...">
            <div class="input-group-append">
                <div class="btn-group">
                    <button class="btn bg-primary text-white" type="button">Search</button>
                    <button class="btn bg-primary text-white dropdown-toggle dropdown-toggle-split" id="searchAdvancedFilter" data-toggle="dropdown" data-boundary="window" aria-haspopup="true" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <div class="dropdown-menu dropdown-menu-right p-3 w-100" aria-labelledby="searchAdvancedFilter">
                        <h6 class="dropdown-header">Advanced Settings</h6>
                        <div class="dropdown-divider"></div>
                        <form>
                        <div class="form-group">
                            <label for="option">Option Header</label>
                            <select class="form-control" id="option" name="option">
                                <option>Pick an option</option>
                                <option value="1">Option 1</option>
                                <option value="2">Option 2</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="inputtext">Input Header</label>
                            <input type="text" class="form-control" id="inputtext" name="inputtext">
                        </div>
                        <div class="form-group">
                            <label for="inputcb">Checkbox Header</label>
                            <input type="checkbox" class="form-control" id="inputcb" name="inputcb">
                        </div>
                        <div class="form-group">
                            <label for="inputradio">Radio Header</label>
                            <input type="radio" class="form-control" id="inputradio" name="inputradio">
                        </div>
                        <div class="form-group">
                            <label for="ta">TextArea Header</label>
                            <textarea class="form-control" id="ta" name="ta"></textarea>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
&#13;
&#13;
&#13;

https://www.codeply.com/go/18ACVL2uzn

另一个选择是使用jQuery stopPropagation()方法:https://www.codeply.com/go/4dVvDDpChW

$('.dropdown-menu').click(function(e) {
    e.stopPropagation();
});