如果在内部单击下拉菜单,则不应将其关闭

Dropdown menu should not be closed if it is clicked inside

本文关键字:在内部 单击 下拉菜单 如果      更新时间:2023-09-26

我把一个面板放在引导程序下拉列表中,但当我点击面板内部时,下拉列表就会消失。我在stackoverflow上得到了很多解决方案,但它对我不起作用。当我点击内部时,如何防止面板消失。enter code here

   <span class="input-group-addon dropdown">
      <label class="dropdown-toggle " id="dropdownMenu1" data-toggle="dropdown aria-expanded="false">Advanced Search  <span class="caret"></span></label>
      <div class="panel panel-default panel-body dropdown-menu " role="menu"" aria-labelledby="dropdownMenu1" style="width:860px; margin-left:-700px">
        Panel content
      </div>
   </span> 

试着这样做:如果你点击主体,它会检查父类,如果你点击下拉列表,如果父类有.dropdown-parentclass,那么下拉列表不会关闭。

$('body').click(function(event){
    if ($(event.target).parent('.dropdown-parentclass').size()>0) {
        return false;
    }
});

尝试使用下面的代码。只有当您单击容器的外侧时,它才会关闭下拉列表。

      $(document).on("mouseup", function (e) {
        var container = $(".panel");
        if (!container.is(e.target) // if the target of the click isn't the container
            && container.has(e.target).length === 0) // nor a descendant of the container
        {
            container.hide();
        }
        else {
        }
    });