【问题标题】:HTML select field with filter带有过滤器的 HTML 选择字段
【发布时间】:2013-02-21 22:37:01
【问题描述】:

我有一个选择字段,其中大量已发布的名称可能达到数百个。

我需要的是过滤字段,我的意思是:

如果选择并搜索了一个,则显示为表单的示例文本,并且可以编写新的搜索删除之前搜索的示例文本。

在框中键入文本时,栅栏将显示已过滤的文本选项列表。

一个例子是:如果我在文本框中输入 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>

【问题讨论】:

  • 有可用的 jQuery 自动完成脚本。拿一个。
  • jquery 自动完成:jqueryui.com/autocomplete
  • 我使用了示例,但无法删除消息: attr( "title", value + " didn't match any item" ) and .attr( "title", "Show All Items " )

标签: javascript jquery html


【解决方案1】:

试试这个。

Chosen 是一个用于&lt;select&gt; html 标签的 jQuery 插件。

它不仅让你的选择框看起来更漂亮,而且在选择框的顶部添加了一个非常棒的搜索功能。

源代码/演示在这里找到:http://harvesthq.github.com/chosen/

【讨论】:

    【解决方案2】:

    我猜这正是您要找的东西

    /* 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>

    【讨论】:

      【解决方案3】:

      如果你想使用 jQuery,那么这些是很好的 UI 选项:

      https://select2.org/dropdown

      https://jqueryui.com/selectmenu/

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


      如果你想使用 Vanilla JavaScript(没有 jQuery)。这是最好的选择。

      您可以根据自己的选择自定义颜色和按钮布局。

      More info

      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>

      【讨论】:

        猜你喜欢
        • 2020-10-09
        • 2019-04-09
        • 2022-01-09
        • 2016-07-22
        • 2015-03-12
        • 1970-01-01
        • 2013-10-20
        • 1970-01-01
        • 2019-06-18
        相关资源
        最近更新 更多