dqhgjay5753 2018-07-12 10:06
浏览 101

单击单选按钮的标签时,下拉列表消失

I'm making a search filter rendered as a dropup: it's a dropdown - presented as a bootstrap dropup - with some checkboxes (plus labels) inside.

If I press the checkboxes everything works fine, but whenever I press a checkbox's label instead, the whole dropup turns blue while the mouse is down and disappears when the mouse is up. The radio button is actually selected, but then I have to re-open the dropup to keep on selecting the other radio buttons I wish to select.

Here's the code:

<!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>

Thanks in advance for any kind of help and please tell me if you need more info.

  • 写回答

2条回答 默认 最新

  • dongmu3269 2018-07-12 11:03
    关注

    Please try this (NB: you may find better solution than this. Checked in Chrome v65)

    You need to have the below JavaScript code to make it work.

    $('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>

    A different approach

    $(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>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计