简体   繁体   English

单击时引导程序下拉关闭

[英]Bootstrap dropdown closing when clicked

I put a form inside a bootstrap dropdown, but when I click any of the fields in the form, the dropdown goes away. 我在一个bootstrap下拉列表中放了一个表单,但是当我单击表单中的任何字段时,下拉列表就会消失。 I have this piece of code but I don't know where to put it to prevent the dropdown disappearing. 我有这段代码,但我不知道在哪里放置它以防止下拉消失。

$(function test() {
  // Setup drop down menu
  $('.dropdown-toggle').dropdown();

  // Fix input element click problem
  $('.dropdown input, .dropdown label').click(function(e) {
    e.stopPropagation();
  });
});

Could someone please tell me where I should put this? 有人可以告诉我应该把它放在哪里吗? I tried in the bootstrap-dropdown, I tried within the HTML but it still doesn't work. 我尝试了bootstrap-dropdown,我在HTML中试过但它仍然无效。

You can omit the dropdown call all together, the bootstrap dropdown works without it. 您可以一起省略下拉调用,引导下拉列表可以在没有它的情况下工作。 Make sure you're wrapping your script properly, you can include it on the header or body of your page, although personally i prefer placing it on the body of your page. 确保正确包装脚本,您可以将其包含在页面的标题或正文中,但我个人更喜欢将其放在页面正文中。

JS JS

<script type="text/javascript">
    $('.dropdown-menu input, .dropdown-menu label').click(function(e) {
        e.stopPropagation();
    });
</script>

If you want to stop closing only some of dropdown menu, not all of them, then just add an additional class to your ul block: 如果你想只停止关闭一些下拉菜单,而不是全部关闭,那么只需在ul块中添加一个额外的类:

<ul class="dropdown-menu keep-open-on-click">

And use this code: 并使用此代码:

$(document).on('click', '.dropdown-menu', function(e) {
    if ($(this).hasClass('keep-open-on-click')) { e.stopPropagation(); }
});

Time passed since answer had been accepted, but if you want to keep the dropdown opened, if it acts like a kind of dialog, the best way i think is: 答案被接受后已经过了一段时间,但是如果你想保持下拉列表打开,如果它就像一种对话,那么我认为最好的方法是:

$('.dropdown').dropdown().on("hide.bs.dropdown", function(e) {
            if ($.contains(dropdown, e.target)) {
                e.preventDefault();
            //or return false;
            }
        });

You need to stop event from bubbling up the DOM tree: 你需要阻止事件冒泡DOM树:

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

event.stopPropagation prevents event from reaching the node where it's eventually handled by Bootstrap hiding menu. event.stopPropagation可防止事件到达Bootstrap隐藏菜单最终处理的节点。

This works for my (lateral sidebar opening a simple link with bootstrap toggle) 这适用于我(横向侧边栏打开一个简单的链接与引导程序切换)

$('.dropdown').find("a").not('.dropdown-toggle').on("click",function(e){
    e.stopImmediatePropagation();
});

Thank you for the above answer, I was having the same issue w/ submenus under the dropdown menus. 谢谢你的上述答案,我在下拉菜单下遇到了同样的问题w / submenus。 Using the above solution I was able to solve the problem for this as well. 使用上述解决方案,我也能够解决这个问题。

$('.dropdown-menu .dropdown-submenu a[data-toggle="dropdown-submenu"]').click(function (e)
{                   
    e.stopPropagation();
});

Put

<script type="text/javascript">
    $('.dropdown-menu').click(function(e) {
          e.stopPropagation();
    });
</script>

on the original answer, remove all the classes, and just put the ".dropdown-menu" class, it worked for me. 在原来的答案,删除所有的类,并只是把“.dropdown-menu”类,它对我有用。 i have an input field inside the dropdown menu. 我在下拉菜单中有一个输入字段。

菜单截图

If you look to the bottom of sources of dropdown widget, you will see this: 如果您查看下拉窗口小部件源代码的底部,您将看到:

$(document)
  .on('click.bs.dropdown.data-api', '.dropdown form', function(e) {
    e.stopPropagation()
  })

So you have the following choices: 所以你有以下选择:

  1. Simply wrap yor dropdown with form 只需用表格包装你的下拉菜单
  2. Or you can add event listener for click event of .dropdown YOUR_SELECTOR 或者,您可以为.dropdown YOUR_SELECTOR的点击事件添加事件侦听
$(document)
  .on('click.my', '.dropdown some_selector', function(e) {
    e.stopPropagation()
  })

Simply use this example.This solution works for me. 只需使用此示例。此解决方案适用于我。

<script type="text/javascript">
window.addEvent('domready',function() {    
   Element.prototype.hide = function() {
      $(function () { 
        // replace your tab id with myTab
        //<ul id="myTab" class="nav nav-tabs">
        $('#myTab li:eq(1) a').tab('show');
      });      
   };
});
</script>

Hope it'll help. 希望它会有所帮助。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM