Bootstrap4 dropdown only works in the second click

后端 未结 7 1046
失恋的感觉
失恋的感觉 2021-02-19 07:01

I am working on a django project, that is using bootstrap4 and I\'m with a little problem with dropdown toggler.

The toggler only toggles the dropdown-menu after the seco

相关标签:
7条回答
  • 2021-02-19 07:17

    I had the same issue, it occurred because i imported bootstrap library twice. After removing one of them it worked well. Hope it help you.

    0 讨论(0)
  • 2021-02-19 07:25

    I came across the same issue recently, i solved it by writing custom script:

    <div class="filter-dropdown text-right">
      <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Edit</button>
      <div class="dropdown-menu">
         <a class="dropdown-item" href="#">Link 1</a>
         <a class="dropdown-item" href="#">Link 2</a>
         <a class="dropdown-item" href="#">Link 3</a>
      </div>
    </div>
    
    
    
    $(".filter-dropdown").on("click", ".dropdown-toggle", function(e) { 
        e.preventDefault();
        $(this).parent().addClass("show");
        $(this).attr("aria-expanded", "true");
        $(this).next().addClass("show"); 
      });
    
    0 讨论(0)
  • 2021-02-19 07:27

    you need to use the toggle like that and assign the data-target to the drop down menu with it's id

    using

    data-target="#navbarDropdown"

     <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarDropdown" aria-controls="navbarDropdown" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
    

    and then assign that id to the menu

            <li class="nav-item dropdown show">
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarDropdown" aria-controls="navbarDropdown" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
                <div id="navbarDropdown" class="dropdown-menu"  role="menu" aria-labelledby="navbarDropdown">
                 <a class="dropdown-item">Link 1</a>
                 <a class="dropdown-item">Link 2</a>
                </div>
            </li>
    
    0 讨论(0)
  • 2021-02-19 07:32

    I came across the same issue recently, i solved this issue by adding $('.dropdown-toggle').dropdown() after loading the drop-down. Hope this may help without changing from the data-toggle="dropdown" to data-toggle="collapse".

    0 讨论(0)
  • 2021-02-19 07:35

    Try to put bootstrap js file before bootstrap css

    Ex:

    1. Link Bootstrap.min.js file first.

      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
     

    1. Then link css file

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

    I got the same issue and after changing the positions of the js and css, dropdown works well. Hope it helped you.

    0 讨论(0)
  • 2021-02-19 07:37

    I had the same problem then resolved. Often I due to a double import of bootstrap js libraries. Hope will help you.

    0 讨论(0)
提交回复
热议问题