且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

bootstrap-select在Bootstrap下拉菜单中不起作用

更新时间:2023-01-24 10:56:46

您的选择框已打开,但菜单已隐藏,因此要避免这种情况,应手动控制菜单的打开/关闭.因此,每当单击菜单时,从 dropdown 以及 dropdown-menu 中添加/删除类 show .

Your select-box gets open but the menu gets hide so to avoid this one way would be controlling opening/closing of menu manually . So, whenever menu is clicked add/remove class show from dropdown as well from dropdown-menu .

演示代码 :

$(document).ready(function() {
  $('#test-select').selectpicker();
  $('#test-select2').selectpicker();
//onclick of dropdown toggle 
  $('#myDD > a.dropdown-toggle').on('click', function(event) {
    $(this).parent().toggleClass('show')//addor remove class
    $(this).attr('aria-expanded', $(this).attr('aria-expanded') == 'false' ? 'true' : 'false'); //add true or false
    $("div[aria-labelledby=" + $(this).attr('id') + "]").toggleClass('show') //add class/remove
  });

  $('body').on('click', function(e) {
  //check if the click occur outside `myDD` tag if yes ..hide menu
    if (!$('#myDD').is(e.target) &&
      $('#myDD').has(e.target).length === 0 &&
      $('.show').has(e.target).length === 0
    ) {
     //remove clases and add attr 
      $('#myDD').removeClass('show')
      $('#myDD > a').attr('aria-expanded', 'false');
      $("#myDD").children('div.dropdown-menu').removeClass('show')
    }
  });
});

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
<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>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>

<div class="dropdown mb-4 text-dark" id="myDD">
  <a class="btn dropdown-toggle text-muted btn-block btn-grey py-2 font-weight-bold " style="color: black !important; font-size: .8em;" href="#" role="button" id="dropdownMenuLink" aria-haspopup="true" aria-expanded="false" data-display="static">
    <i class="fas fa-sliders-h mr-2"></i> Menu
  </a>
  <div class="dropdown-menu bg-transparent border-0 mt-2" aria-labelledby="dropdownMenuLink" style="position: relative; float: none;">
    <form>
      <div class="input-group mb-3">
        <label for="inputGroupSelect05" class="text-dark d-block w-100 mb-1">Input text</label>
        <input type="text" class="form-control form-control-sm" placeholder="text input">
      </div>

      <div class="input-group mb-3 multi-select ">
        <label for="inputGroupSelect02" class="text-dark d-block w-100 mb-1">Click causes close of dropdown</label>
        <select style="color: #495057 !important;" class="w-100" multiple data-selected-text-format="count" data-style="custom-select custom-select-sm" id="test-select">
          <option>1</option>
          <option>2</option>
          <option selected>3</option>
          <option selected>4</option>
          <option selected>5</option>
        </select>
      </div>

    </form>

  </div>
</div>

<div class="input-group mb-3 multi-select ">
  <label for="inputGroupSelect02" class="text-dark d-block w-100 mb-1">Click causes close of dropdown if open</label>
  <select style="color: #495057 !important;" class="w-100" multiple data-selected-text-format="count" data-style="custom-select custom-select-sm" id="test-select2">
    <option>1</option>
    <option>2</option>
    <option selected>3</option>
    <option selected>4</option>
    <option selected>5</option>
  </select>
</div>