簡體   English   中英

HTML select 字段帶過濾器

[英]HTML select field with filter

我有一個 select 字段,其中發布的大量名稱可能達到數百個。

我需要的是過濾字段,我的意思是:

如果選擇並搜索了一個,則顯示為 forms 的示例文本,並且可以編寫新的搜索刪除之前搜索的示例文本。

在框中鍵入文本時,該柵欄將顯示已過濾的文本選項列表。

一個例子是:如果我在文本框中輸入 D 或 d,它會顯示 Daniel Diego 的選項列表,所以對於所有,如果您使用 Diego 進行搜索,那么在加載搜索文本框后會顯示為示例 Diego。

<select id="id_name" name="name">
    <option value="3">Diego </option>
    <option value="4">Daniel  </option>
    <option value="5">Fernando  </option>
    <option value="6">Luz </option>
    <option value="7">Catherine  </option>
    <option value="8">Samuel  </option>
    <option value="10">Eduardo  </option>
</select>

試試這個。

Chosen 是<select> html 標簽的 jQuery 插件。

它不僅使您的選擇框看起來更好看,而且在選擇框的頂部添加了一個非常好的搜索功能。

源/演示在這里找到: http : //harvesthq.github.com/chosen/

我想這正是你要找的

 /* When the user clicks on the button, toggle between hiding and showing the dropdown content */ function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } function filterFunction() { var input, filter, ul, li, a, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); div = document.getElementById("myDropdown"); a = div.getElementsByTagName("a"); for (i = 0; i < a.length; i++) { txtValue = a[i].textContent || a[i].innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { a[i].style.display = ""; } else { a[i].style.display = "none"; } } }
 /* Dropdown Button */ .dropbtn { background-color: #04AA6D; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } /* Dropdown button on hover & focus */ .dropbtn:hover, .dropbtn:focus { background-color: #3e8e41; } /* The search field */ #myInput { box-sizing: border-box; background-image: url('searchicon.png'); background-position: 14px 12px; background-repeat: no-repeat; font-size: 16px; padding: 14px 20px 12px 45px; border: none; border-bottom: 1px solid #ddd; } /* The search field when it gets focus/clicked on */ #myInput:focus {outline: 3px solid #ddd;} /* The container <div> - needed to position the dropdown content */ .dropdown { position: relative; display: inline-block; } /* Dropdown Content (Hidden by Default) */ .dropdown-content { display: none; position: absolute; background-color: #f6f6f6; min-width: 230px; border: 1px solid #ddd; z-index: 1; } /* Links inside the dropdown */ .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } /* Change color of dropdown links on hover */ .dropdown-content a:hover {background-color: #f1f1f1} /* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ .show {display:block;}
 <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Dropdown</button> <div id="myDropdown" class="dropdown-content"> <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()"> <a href="#about">About</a> <a href="#base">Base</a> <a href="#blog">Blog</a> <a href="#contact">Contact</a> <a href="#custom">Custom</a> <a href="#support">Support</a> <a href="#tools">Tools</a> </div> </div>

如果你想使用 jQuery,那么這些是很好的 UI 選項:

https://select2.org/dropdown

https://jqueryui.com/selectmenu/

https://harvesthq.github.io/chosen/


如果你想使用 Vanilla JavaScript(沒有 jQuery)。 這是最好的選擇。

您可以根據自己的選擇自定義顏色和按鈕布局。

更多信息

 function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } function filterFunction() { var input, filter, ul, li, a, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); div = document.getElementById("myDropdown"); a = div.getElementsByTagName("a"); for (i = 0; i < a.length; i++) { txtValue = a[i].textContent || a[i].innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { a[i].style.display = ""; } else { a[i].style.display = "none"; } } }
 .dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } .dropbtn:hover, .dropbtn:focus { background-color: #3e8e41; } #myInput { box-sizing: border-box; background-image: url('searchicon.png'); background-position: 14px 12px; background-repeat: no-repeat; font-size: 16px; padding: 14px 20px 12px 45px; border: none; border-bottom: 1px solid #ddd; } #myInput:focus {outline: 3px solid #ddd;} .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f6f6f6; min-width: 230px; overflow: auto; border: 1px solid #ddd; z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown a:hover {background-color: #ddd;} .show {display: block;}
 <!DOCTYPE html> <h2>Basic Select Search/Filter Dropdown</h2> <p> Click to open the dropdown menu, and use the input field to search for a specific dropdown link. </p> <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Select Here</button> <div id="myDropdown" class="dropdown-content"> <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()"> <a href="#about">About</a> <a href="#base">Base</a> <a href="#blog">Blog</a> <a href="#contact">Contact</a> <a href="#custom">Custom</a> <a href="#support">Support</a> <a href="#tools">Tools</a> </div> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM