基于首字母的过滤搜索

问题是我想以首字母在前的格式过滤列表中的单词。


正如您现在所看到的,我在搜索字段中输入字母“ a ”,结果是所有与“ a ”相关的单词。但我只想要“ a ”的第一个字母单词。


然后我输入“ an ”,结果应该是“ an ”起始词。


function myFunction() {

    var input, filter, ul, li, a, i, txtValue;

    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];

        txtValue = a.textContent || a.innerText;

        if (txtValue.toUpperCase().indexOf(filter) > -1) {

            li[i].style.display = "";

        } else {

            li[i].style.display = "none";

        }

    }

}

<!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">


</head>

<body>


<h2>My Phonebook</h2>


<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">


<ul id="myUL">

  <li><a href="#">Adele</a></li>

  <li><a href="#">Agnes</a></li>

  <li><a href="#">Agitha</a></li>


  <li><a href="#">Billy</a></li>

  <li><a href="#">Bob</a></li>


  <li><a href="#">Calvin</a></li>

  <li><a href="#">Christina</a></li>

  <li><a href="#">Cindy</a></li>

</ul>




</body>

</html>

https://i.stack.imgur.com/yAXJP.png


皈依舞
浏览 211回答 6
6回答

子衿沉夜

这if (txtValue.toUpperCase().indexOf(filter) > -1)应该改为if (txtValue.toUpperCase().indexOf(filter) === 0)function myFunction() {&nbsp; &nbsp; var input, filter, ul, li, a, i, txtValue;&nbsp; &nbsp; input = document.getElementById("myInput");&nbsp; &nbsp; filter = input.value.toUpperCase();&nbsp; &nbsp; ul = document.getElementById("myUL");&nbsp; &nbsp; li = ul.getElementsByTagName("li");&nbsp; &nbsp; for (i = 0; i < li.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; a = li[i].getElementsByTagName("a")[0];&nbsp; &nbsp; &nbsp; &nbsp; txtValue = a.textContent || a.innerText;&nbsp; &nbsp; &nbsp; &nbsp; if (txtValue.toUpperCase().indexOf(filter) === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; li[i].style.display = "";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; li[i].style.display = "none";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><h2>My Phonebook</h2><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"><ul id="myUL">&nbsp; <li><a href="#">Adele</a></li>&nbsp; <li><a href="#">Agnes</a></li>&nbsp; <li><a href="#">Agitha</a></li>&nbsp; <li><a href="#">Billy</a></li>&nbsp; <li><a href="#">Bob</a></li>&nbsp; <li><a href="#">Calvin</a></li>&nbsp; <li><a href="#">Christina</a></li>&nbsp; <li><a href="#">Cindy</a></li></ul></body></html>

蛊毒传说

你可以用startsWith它代替。if (txtValue.toUpperCase().startsWith(filter))

holdtom

&nbsp;// this function will help you to filter array on the basis of first // letter or wordconst collections = ["Ali","Ali and Asif", "Umar OR usman"];const searchByMultipleWords = (&nbsp; collections,&nbsp; value) => {&nbsp; return collections.filter((collection) => {&nbsp; &nbsp; const collectionName = collection&nbsp; &nbsp; &nbsp; ? collection.toLowerCase()&nbsp; &nbsp; &nbsp; : "";&nbsp; &nbsp; return collectionName.startsWith(value.toLowerCase());&nbsp; });};console.log(searchByMultipleWords(collections,"ali"))

慕尼黑的夜晚无繁华

您可以使用startsWith()函数来获取结果。function myFunction() {&nbsp; var input, filter, ul, li, a, i, txtValue;&nbsp; input = document.getElementById("myInput");&nbsp; filter = input.value.toUpperCase();&nbsp; ul = document.getElementById("myUL");&nbsp; li = ul.getElementsByTagName("li");&nbsp; for (i = 0; i < li.length; i++) {&nbsp; &nbsp; a = li[i].getElementsByTagName("a")[0];&nbsp; &nbsp; txtValue = a.textContent || a.innerText;&nbsp; &nbsp; if (txtValue.toUpperCase().startsWith(filter)) {&nbsp; &nbsp; &nbsp; li[i].style.display = "";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; li[i].style.display = "none";&nbsp; &nbsp; }&nbsp; }}<!DOCTYPE html><html><head>&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1"></head><body>&nbsp; <h2>My Phonebook</h2>&nbsp; <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">&nbsp; <ul id="myUL">&nbsp; &nbsp; <li><a href="#">Adele</a></li>&nbsp; &nbsp; <li><a href="#">Agnes</a></li>&nbsp; &nbsp; <li><a href="#">Agitha</a></li>&nbsp; &nbsp; <li><a href="#">Billy</a></li>&nbsp; &nbsp; <li><a href="#">Bob</a></li>&nbsp; &nbsp; <li><a href="#">Calvin</a></li>&nbsp; &nbsp; <li><a href="#">Christina</a></li>&nbsp; &nbsp; <li><a href="#">Cindy</a></li>&nbsp; </ul></body></html>

杨__羊羊

function myFunction() {&nbsp; &nbsp; var input, filter, ul, li, a, i, txtValue;&nbsp; &nbsp; input = document.getElementById("myInput");&nbsp; &nbsp; filter = input.value.toUpperCase();&nbsp; &nbsp; let filterLength = filter.length;&nbsp; &nbsp; ul = document.getElementById("myUL");&nbsp; &nbsp; li = ul.getElementsByTagName("li");&nbsp; &nbsp; for (i = 0; i < li.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; a = li[i].getElementsByTagName("a")[0];&nbsp; &nbsp; &nbsp; &nbsp; txtValue = a.textContent || a.innerText;&nbsp; &nbsp; &nbsp; &nbsp; if (txtValue.substr(0, filterLength).toUpperCase() === filter) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; li[i].style.display = "";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; li[i].style.display = "none";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

POPMUISE

你应该使用startsWith()而不是indexOf()在你的JS中替换if (txtValue.toUpperCase().indexOf(filter) > -1)和if (txtValue.toUpperCase().startsWith(filter))function myFunction() {  var input, filter, ul, li, a, i, txtValue;  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];    txtValue = a.textContent || a.innerText;    if (txtValue.toUpperCase().startsWith(filter)) {      li[i].style.display = "";    } else {      li[i].style.display = "none";    }  }}* {  box-sizing: border-box;}#myInput {  background-image: url('/css/searchicon.png');  background-position: 10px 12px;  background-repeat: no-repeat;  width: 100%;  font-size: 16px;  padding: 12px 20px 12px 40px;  border: 1px solid #ddd;  margin-bottom: 12px;}#myUL {  list-style-type: none;  padding: 0;  margin: 0;}#myUL li a {  border: 1px solid #ddd;  margin-top: -1px;  /* Prevent double borders */  background-color: #f6f6f6;  padding: 12px;  text-decoration: none;  font-size: 18px;  color: black;  display: block}#myUL li a:hover:not(.header) {  background-color: #eee;}<!DOCTYPE html><html><head>  <meta name="viewport" content="width=device-width, initial-scale=1"></head><body>  <h2>My Phonebook</h2>  <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">  <ul id="myUL">    <li><a href="#">Adele</a></li>    <li><a href="#">Agnes</a></li>    <li><a href="#">Agitha</a></li>    <li><a href="#">Billy</a></li>    <li><a href="#">Bob</a></li>    <li><a href="#">Calvin</a></li>    <li><a href="#">Christina</a></li>    <li><a href="#">Cindy</a></li>  </ul></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript