Bootstrap dropdown closing when clicked

后端 未结 9 2389
南旧
南旧 2020-11-29 02:00

I put a form inside a bootstrap dropdown, but when I click any of the fields in the form, the dropdown goes away. I have this piece of code but I don\'t know where to put it

相关标签:
9条回答
  • 2020-11-29 02:08

    You need to stop event from bubbling up the DOM tree:

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

    event.stopPropagation prevents event from reaching the node where it's eventually handled by Bootstrap hiding menu.

    0 讨论(0)
  • 2020-11-29 02:09

    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();
    });
    
    0 讨论(0)
  • 2020-11-29 02:12

    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. i have an input field inside the dropdown menu.

    Screenshot of menu

    0 讨论(0)
  • 2020-11-29 02:17

    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.

    0 讨论(0)
  • 2020-11-29 02:17

    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;
                }
            });
    
    0 讨论(0)
  • 2020-11-29 02:23

    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 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(); }
    });
    
    0 讨论(0)
提交回复
热议问题