【问题标题】:Dropdown disappears when a radio button's label is clicked单击单选按钮的标签时下拉菜单消失
【发布时间】:2018-07-12 10:06:43
【问题描述】:

我正在制作一个呈现为下拉菜单的搜索过滤器:它是一个下拉菜单 - 以引导下拉菜单的形式呈现 - 里面有一些复选框(加上标签)。

如果我按下复选框,一切正常,但每当我按下复选框的标签时,整个下拉菜单会在鼠标按下时变为蓝色,并在鼠标升起时消失。单选按钮实际上已被选中,但我必须重新打开下拉菜单才能继续选择我希望选择的其他单选按钮。

代码如下:

<!DOCTYPE html>
<html>

<body>
  <section id="homeSearchSection" class="search homeSearch">
  <div id="homeSearchDiv" class="container text-center">
    <div id="homeSearchDiv" class="form-holder">
      <h2>Onde comer?</h2>
      <p class="lead">Use o nome do estabelecimento ou a sua localização.</p>
      <form id="homeSearch" action="#" method="post">
        <div class="form-group row" id="formGroupHome">
          <div class="col-lg-9 col-md-12">
            <input class="searchInput" type="text" name="search" id="search" placeholder="Procure aqui...">
            <button type="submit" class="btn btn-primary btn-gradient submit homeSearchSubmit"><span style="font-size: 24px;" class="icon-loupe"></span></button>
          </div>
          <div class="col-3 dropup dropdownHome">
            <button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">Pesquisa avançada</button>
            <div class="dropdown-menu dropdown-menu-right">
              <div class="dropdown-item">
                <div class="row">
                  <div class="col" style="border-right: solid #0084FF 1px;">
                    <h6 class="dropdown-header">Gastronomia:</h6>
                    <label class="dropdown-item"><input type="checkbox"> Italiana</label>
                    <label class="dropdown-item"><input type="checkbox"> Chinesa</label>
                    <label class="dropdown-item"><input type="checkbox"> Portuguesa</label>
                    <label class="dropdown-item"><input type="checkbox"> Mexicana</label>
                    <label class="dropdown-item"><input type="checkbox"> Brasileira</label>
                  </div>
                  <div class="col" style="border-right: solid #0084FF 1px;">
                    <h6 class="dropdown-header">Regime alimentar:</h6>
                    <label class="dropdown-item"><input type="checkbox"> Vegetariano</label>
                    <label class="dropdown-item"><input type="checkbox"> Vegan</label>
                    <label class="dropdown-item"><input type="checkbox"> Macrobiótico</label>
                  </div>
                  <div class="col">
                    <h6 class="dropdown-header">Tipo de comida:</h6>
                    <label class="dropdown-item"><input type="checkbox"> Carne</label>
                    <label class="dropdown-item"><input type="checkbox"> Peixe</label>
                    <label class="dropdown-item"><input type="checkbox"> Kebab</label>
                    <label class="dropdown-item"><input type="checkbox"> Picanha</label>
                    <label class="dropdown-item"><input type="checkbox"> Marisco</label>
                    <label class="dropdown-item"><input type="checkbox"> Hambúrguer</label>
                  </div>
                  <div class="col" style="border-right: solid #0084FF 1px;">
                    <h6 class="dropdown-header">&nbsp;</h6>
                    <label class="dropdown-item"><input type="checkbox"> Pizza</label>
                    <label class="dropdown-item"><input type="checkbox"> Francesinha</label>
                    <label class="dropdown-item"><input type="checkbox"> Sandes</label>
                  </div>
                  <div class="col" style="border-right: solid #0084FF 1px;">
                    <h6 class="dropdown-header">Tipo de refeição:</h6>
                    <label class="dropdown-item"><input type="checkbox"> Gourmet</label>
                    <label class="dropdown-item"><input type="checkbox"> Self-service</label>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
  </section>
</body>

</html>

提前感谢您的任何帮助,如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: php html twitter-bootstrap bootstrap-4


    【解决方案1】:

    请试试这个(注意:你可能会找到比这更好的解决方案。在 Chrome v65 中检查)

    您需要拥有以下 JavaScript 代码才能使其工作。

    $('body').on('hide.bs.dropdown', '.dropdownHome', function (e) {
      if(event.target.nodeName == "LABEL")  {
        return false;
      }
    });
    

    $('body').on('hide.bs.dropdown', '.dropdownHome', function (e) {
      if(event.target.nodeName == "LABEL")  return false;
    });
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
    
      <section id="homeSearchSection" class="search homeSearch">
      <div id="homeSearchDiv" class="container text-center">
        <div id="homeSearchDiv" class="form-holder">
          <h2>Onde comer?</h2>
          <p class="lead">Use o nome do estabelecimento ou a sua localização.</p>
          <form id="homeSearch" action="#" method="post">
            <div class="form-group row" id="formGroupHome">
              <div class="col-lg-9 col-md-12">
                <input class="searchInput" type="text" name="search" id="search" placeholder="Procure aqui...">
                <button type="submit" class="btn btn-primary btn-gradient submit homeSearchSubmit"><span style="font-size: 24px;" class="icon-loupe"></span></button>
              </div>
              <div class="col-3 dropup dropdownHome">
                <button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">Pesquisa avançada</button>
                <div class="dropdown-menu dropdown-menu-right">
                  <div class="dropdown-item">
                    <div class="row">
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">Gastronomia:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Italiana</label>
                        <label class="dropdown-item"><input type="checkbox"> Chinesa</label>
                        <label class="dropdown-item"><input type="checkbox"> Portuguesa</label>
                        <label class="dropdown-item"><input type="checkbox"> Mexicana</label>
                        <label class="dropdown-item"><input type="checkbox"> Brasileira</label>
                      </div>
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">Regime alimentar:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Vegetariano</label>
                        <label class="dropdown-item"><input type="checkbox"> Vegan</label>
                        <label class="dropdown-item"><input type="checkbox"> Macrobiótico</label>
                      </div>
                      <div class="col">
                        <h6 class="dropdown-header">Tipo de comida:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Carne</label>
                        <label class="dropdown-item"><input type="checkbox"> Peixe</label>
                        <label class="dropdown-item"><input type="checkbox"> Kebab</label>
                        <label class="dropdown-item"><input type="checkbox"> Picanha</label>
                        <label class="dropdown-item"><input type="checkbox"> Marisco</label>
                        <label class="dropdown-item"><input type="checkbox"> Hambúrguer</label>
                      </div>
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">&nbsp;</h6>
                        <label class="dropdown-item"><input type="checkbox"> Pizza</label>
                        <label class="dropdown-item"><input type="checkbox"> Francesinha</label>
                        <label class="dropdown-item"><input type="checkbox"> Sandes</label>
                      </div>
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">Tipo de refeição:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Gourmet</label>
                        <label class="dropdown-item"><input type="checkbox"> Self-service</label>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
      </section>

    另一种方法

    $(document).click(function(e) {
     if($(e.target).closest(".dropdownHome").length === 0 ) {
        $('.dropdown-menu').removeClass('show');
     }
    });
    
    $('.dropdownHome').on("hide.bs.dropdown", function() {
        return false;
    });
    

    $(document).click(function(e) {
     if($(e.target).closest(".dropdownHome").length === 0 ) {
        $('.dropdown-menu').removeClass('show');
     }
    });
    
    $('.dropdownHome').on("hide.bs.dropdown", function() {
        return false;
    });
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
    
      <section id="homeSearchSection" class="search homeSearch">
      <div id="homeSearchDiv" class="container text-center">
        <div id="homeSearchDiv" class="form-holder">
          <h2>Onde comer?</h2>
          <p class="lead">Use o nome do estabelecimento ou a sua localização.</p>
          <form id="homeSearch" action="#" method="post">
            <div class="form-group row" id="formGroupHome">
              <div class="col-lg-9 col-md-12">
                <input class="searchInput" type="text" name="search" id="search" placeholder="Procure aqui...">
                <button type="submit" class="btn btn-primary btn-gradient submit homeSearchSubmit"><span style="font-size: 24px;" class="icon-loupe"></span></button>
              </div>
              <div class="col-3 dropup dropdownHome">
                <button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">Pesquisa avançada</button>
                <div class="dropdown-menu dropdown-menu-right">
                  <div class="dropdown-item">
                    <div class="row">
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">Gastronomia:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Italiana</label>
                        <label class="dropdown-item"><input type="checkbox"> Chinesa</label>
                        <label class="dropdown-item"><input type="checkbox"> Portuguesa</label>
                        <label class="dropdown-item"><input type="checkbox"> Mexicana</label>
                        <label class="dropdown-item"><input type="checkbox"> Brasileira</label>
                      </div>
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">Regime alimentar:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Vegetariano</label>
                        <label class="dropdown-item"><input type="checkbox"> Vegan</label>
                        <label class="dropdown-item"><input type="checkbox"> Macrobiótico</label>
                      </div>
                      <div class="col">
                        <h6 class="dropdown-header">Tipo de comida:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Carne</label>
                        <label class="dropdown-item"><input type="checkbox"> Peixe</label>
                        <label class="dropdown-item"><input type="checkbox"> Kebab</label>
                        <label class="dropdown-item"><input type="checkbox"> Picanha</label>
                        <label class="dropdown-item"><input type="checkbox"> Marisco</label>
                        <label class="dropdown-item"><input type="checkbox"> Hambúrguer</label>
                      </div>
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">&nbsp;</h6>
                        <label class="dropdown-item"><input type="checkbox"> Pizza</label>
                        <label class="dropdown-item"><input type="checkbox"> Francesinha</label>
                        <label class="dropdown-item"><input type="checkbox"> Sandes</label>
                      </div>
                      <div class="col" style="border-right: solid #0084FF 1px;">
                        <h6 class="dropdown-header">Tipo de refeição:</h6>
                        <label class="dropdown-item"><input type="checkbox"> Gourmet</label>
                        <label class="dropdown-item"><input type="checkbox"> Self-service</label>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
      </section>

    【讨论】:

    • 没有一个选项似乎对我有用...第一个似乎什么都不做,第二个使下拉菜单根本不出现。
    【解决方案2】:

    整个下拉菜单变成蓝色的原因是因为你用dropdown-item 包裹了所有内容。当您按下按钮的标签时,您也在按下该下拉项目。点击事件正在传播并激活其父下拉项。

    如果你看一下默认的bootstrap4风格:

    .dropdown-item.active, .dropdown-item:active {
        color: #fff;
        text-decoration: none;
        background-color: #007bff;
    }
    

    这就是为什么您会看到原色背景。

    如果您想继续使用下拉菜单来包含所有复选框,则需要停止单击事件传播给父级,并更改下拉项的活动样式。

    $(".dropdown-item").click(function(e){
       e.stopPropagation();
    })
    
    .dropdown-item:active {
        background-color: #fff;
    }
    

    小提琴http://jsfiddle.net/aq9Laaew/87023/

    取决于您的应用程序,您可能想尝试在复选框中使用modal 而不是dropdown(顺便说一下,它们不是单选按钮)。

    【讨论】:

      【解决方案3】:

      引导程序 5

      现在有一个更简单的解决方案:对下拉菜单使用“手动关闭”选项,它可以完美地解决这个问题。

      您只需将data-bs-auto-close="false" 添加到下拉切换&lt;button&gt;

      在此处查看示例:Dropdowns Auto close behavior

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        • 1970-01-01
        • 2022-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多