简体   繁体   中英

Bootstrap4 Dropdown Menu auto collapse when clicking select element

this is an extended question I encountered after my previous question .

The problem lies when someone clicked on the arrow to show the advanced searches, it drops down the form as expected. However, the moment when someone clicked on the select drop down, the whole dropdown menu would then collapse on it's own.

I've been trying to find out what element/trigger is causing this issue and see if there's a way to work around this, but I have no progress at all

Here's what I've observed so far

  1. [ Expected ] If the user clicked outside of the drop menu area, the menu collapses
  2. [ Unexpected ] If the user clicked any of the whitespace within the drop menu , the menu still collapses
  3. [ Expected ] if user clicked on the text field, checkbox, radiobox, textarea, they are supposed to be able to type and enter required texts.
  4. [ Unexpected ] If the user clicked a select menu , the dropmenu collapses instantly

I have dropped a sample code down below to demonstrate the question I have.

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <div class="container"> <div class="row"> <div class="col-12"> <div class="input-group dropdown"> <input type="text" class="form-control" id="project_search" placeholder="search ..."> <div class="input-group-append"> <div class="btn-group"> <button class="btn bg-primary text-white" type="button">Search</button> <button class="btn bg-primary text-white dropdown-toggle dropdown-toggle-split" id="searchAdvancedFilter" data-toggle="dropdown" data-boundary="window" aria-haspopup="true" aria-expanded="false"> <span class="caret"></span> <span class="sr-only">Toggle Dropdown</span> </button> <div class="dropdown-menu dropdown-menu-right p-3 w-100" aria-labelledby="searchAdvancedFilter"> <h6 class="dropdown-header">Advanced Settings</h6> <div class="dropdown-divider"></div> <div class="form-group"> <label for="option">Option Header</label> <select class="form-control" id="option" name="option"> <option>Pick an option</option> <option value=1>Option 1</option> <option value=2>Option 2</option> </select> </div> <div class="form-group"> <label for="inputtext">Input Header</label> <input type="text" class="form-control" id="inputtext" name="inputtext"> </div> <div class="form-group"> <label for="inputcb">Checkbox Header</label> <input type="checkbox" class="form-control" id="inputcb" name="inputcb"> </div> <div class="form-group"> <label for="inputradio">Radio Header</label> <input type="radio" class="form-control" id="inputradio" name="inputradio"> </div> <div class="form-group"> <label for="ta">TextArea Header</label> <textarea class="form-control" id="ta" name="ta"></textarea> </div> </div> </div> </div> </div> </div> </div> </div> 

Bootstrap Dropdown menus will always close on click, except for when a <form> is used. The way to prevent it from closing on click is to use a <form> element inside dropdown...

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <div class="container"> <div class="row"> <div class="col-12"> <div class="input-group dropdown"> <input type="text" class="form-control" id="project_search" placeholder="search ..."> <div class="input-group-append"> <div class="btn-group"> <button class="btn bg-primary text-white" type="button">Search</button> <button class="btn bg-primary text-white dropdown-toggle dropdown-toggle-split" id="searchAdvancedFilter" data-toggle="dropdown" data-boundary="window" aria-haspopup="true" aria-expanded="false"> <span class="caret"></span> <span class="sr-only">Toggle Dropdown</span> </button> <div class="dropdown-menu dropdown-menu-right p-3 w-100" aria-labelledby="searchAdvancedFilter"> <h6 class="dropdown-header">Advanced Settings</h6> <div class="dropdown-divider"></div> <form> <div class="form-group"> <label for="option">Option Header</label> <select class="form-control" id="option" name="option"> <option>Pick an option</option> <option value="1">Option 1</option> <option value="2">Option 2</option> </select> </div> <div class="form-group"> <label for="inputtext">Input Header</label> <input type="text" class="form-control" id="inputtext" name="inputtext"> </div> <div class="form-group"> <label for="inputcb">Checkbox Header</label> <input type="checkbox" class="form-control" id="inputcb" name="inputcb"> </div> <div class="form-group"> <label for="inputradio">Radio Header</label> <input type="radio" class="form-control" id="inputradio" name="inputradio"> </div> <div class="form-group"> <label for="ta">TextArea Header</label> <textarea class="form-control" id="ta" name="ta"></textarea> </div> </form> </div> </div> </div> </div> </div> </div> </div> 

https://www.codeply.com/go/18ACVL2uzn


Another option is to use the jQuery stopPropagation() method: https://www.codeply.com/go/4dVvDDpChW

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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