按首字母过滤列表
Filter a list by the first letter(s)
我有一个郊区列表,我正在使用以下内容进行过滤。我工作得很好,但我希望它从起始字母过滤而不是匹配任何字母。
我觉得这样的事情应该有效,但没有帮助
if (txtValue.toUpperCase().indexOf(filter) === 0)
if (txtValue.toUpperCase().startsWith(filter))
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("mainNav");
a = div.getElementsByTagName("li");
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";
}
}
}
<ul class="nav nav-pills" id="mainNav">
<input type="text" placeholder="Search..." id="myInput" onkeyup="filterFunction()" style="padding: 10px; background-color:#343434; border: 0px; color: white;">
<?php include_once( 'suburbs.php' );?>
</ul>
对我来说似乎工作正常。
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("mainNav");
a = div.getElementsByTagName("li");
for (i = 0; i < a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().startsWith(filter)) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
<input id="myInput" />
<div id="mainNav">
<li>test1</li>
<li>test2</li>
<li>atcbjgjhg</li>
<li>something</li>
</div>
<button onclick="filterFunction()">
Filter
</button>
我有一个郊区列表,我正在使用以下内容进行过滤。我工作得很好,但我希望它从起始字母过滤而不是匹配任何字母。
我觉得这样的事情应该有效,但没有帮助
if (txtValue.toUpperCase().indexOf(filter) === 0)
if (txtValue.toUpperCase().startsWith(filter))
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("mainNav");
a = div.getElementsByTagName("li");
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";
}
}
}
<ul class="nav nav-pills" id="mainNav">
<input type="text" placeholder="Search..." id="myInput" onkeyup="filterFunction()" style="padding: 10px; background-color:#343434; border: 0px; color: white;">
<?php include_once( 'suburbs.php' );?>
</ul>
对我来说似乎工作正常。
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("mainNav");
a = div.getElementsByTagName("li");
for (i = 0; i < a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().startsWith(filter)) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
<input id="myInput" />
<div id="mainNav">
<li>test1</li>
<li>test2</li>
<li>atcbjgjhg</li>
<li>something</li>
</div>
<button onclick="filterFunction()">
Filter
</button>