搜索/过滤器下拉菜单:单击

时间:2018-09-26 12:59:41

标签: javascript html

我刚刚按照以下指南实施了“搜索/过滤器”下拉菜单:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown_filter

<h2>Search/Filter Dropdown</h2>
        <p>Click on the button 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">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
            <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
          </div>
        </div>

此搜索/过滤器下拉列表将allNameMuseums()方法的数组作为值,即[“ ACQUARIUM”,“ Museo2”,“ Museo3”]。

 document.addEventListener("DOMContentLoaded", function(event) {
      allNameMuseums().forEach(function(item) {
        document.getElementById("myDropdown").innerHTML += '<a href="' + item + '">' + item + '</a>';
      })
    });


    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++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
          a[i].style.display = "";
        } else {
          a[i].style.display = "none";
        }
      }
    }

    function allNameMuseums() {
      return ["ACQUARIO", "Museo2", "Museo3"];
    }

我有两个异步方法,specificoMuseo(name)和allMuseums(),以及一个变量var data = proof;,它使用allMuseums()方法的结果。

specificoMuseo(name).then(proof2 => {
            allMuseums().then(proof => {

var data = proof;

 });
    });

我希望当我单击“搜索/过滤器下拉列表”的ACQUARIO时,变量“数据”将specificoMuseo(“ ACQUARIO”)的结果作为输入。

1 个答案:

答案 0 :(得分:0)

您需要使用以下类似的逻辑,

var name; //globle variable so you can access in your method 

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

function fnclick(obj) {
  name = obj.innerHTML;
  alert(name);
  console.log(name);
}

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++) {
    if (a[i].innerHTML.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 {
    border-box: box-sizing;
    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;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Search/Filter Dropdown</h2>
<p>Click on the button 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">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#about" onclick="fnclick(this)">About</a>
    <a href="#base" onclick="fnclick(this)">Base</a>
    <a href="#blog" onclick="fnclick(this)">Blog</a>
  </div>
</div>