最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to made a searchable drop down - Stack Overflow

programmeradmin2浏览0评论

So i made a dropdown using bootstrapp.

The code i took here : .0/ponents/dropdowns/

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

Now i want to add a search box inside the dropdown list , so that people can type in and it will shorten to a more proper dropdown list.

I try to find many ways , some of them using external plugin like Select2, however , they need a dropdown coded by using <select> and <option> tag . In contrast , my code use button and built-in dropdown class of Bootstrapp.

Anyone please help me .

So i made a dropdown using bootstrapp.

The code i took here : http://getbootstrap./docs/4.0/ponents/dropdowns/

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

Now i want to add a search box inside the dropdown list , so that people can type in and it will shorten to a more proper dropdown list.

I try to find many ways , some of them using external plugin like Select2, however , they need a dropdown coded by using <select> and <option> tag . In contrast , my code use button and built-in dropdown class of Bootstrapp.

Anyone please help me .

Share Improve this question asked Sep 27, 2017 at 8:52 Jake LamJake Lam 3,4723 gold badges29 silver badges52 bronze badges 3
  • Do you want the dropdown list to filter based on the input in the search field? Or do you have problems with the html, to put a input field in a dropdown? – Martin Lindgren Commented Sep 27, 2017 at 11:58
  • yes i want the dropdown list to filter based on the input search – Jake Lam Commented Sep 28, 2017 at 3:14
  • What's the relation with Jira in this question? – Maitrey684 Commented Sep 28, 2017 at 5:30
Add a ment  | 

2 Answers 2

Reset to default 1

When I want to sort lists, I use list.js. Here is an example of how you could solve your problem with having a Boostrap-4 dropdown with a list that you can filter:

https://jsfiddle/o9b17p2d/29/

HTML:

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenu2" id="dropdown-sorted-list">
    <ul class="dropdown-menu-list list">
      <li>
        <button class="dropdown-item dropdown-item-name" type="button">Action</button>
      </li>
      <li>
        <button class="dropdown-item dropdown-item-name" type="button">Another action</button>
      </li>
    </ul>
    <input type="text" class="form-control dropdown-item search" placeholder="Filter" aria-label="Filter" aria-describedby="basic-addon1">
  </div>
</div>

JS: See Example 5 in the list.js documentation on how the set up list.js.

$(document).ready(function() {
  var options = {
    valueNames: ['dropdown-item-name']
  };

  var hackerList = new List('dropdown-sorted-list', options);
});

CSS:

.dropdown-menu-list {
  list-style-type: none;
  padding-left: 0;
}

As you can read in the Bootstrap 4 documentation for dropdowns, it is possible to use <button> elements in the dropdown.

thanks guys for helping , i have found the solution . ill post here for anyone who needs

function searchBox() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";

        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论