添加另一个 for 循环来评估所有 tds(列)将帮助您。
这可能会有所帮助:
// This variable switch if the filter catch any result
var isset = false;
//This loop evalaute all rows
for (i = 0; i < tr.length; i++) {
//Here we search for all columns in this row
//note that we get rid of [0] at the end because we're looking for all
//tds, not just the first one
let tds = tr[i].getElementsByTagName("td");
//Here we evaluate each column for this row
for (let td of tds) {
//If td has a value
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
//If the filter catch any result, we show the row
isset = true;
}
}
}
//The decision of show or hide the row is made here because
//the loop has to evaluate all columns before decide if show or hide the entire row
if (isset) {
//If the filter catch any result, we show the row
tr[i].style.display = "";
} else {
//If the filter catch no result, we hide the row
tr[i].style.display = "none";
}
//This is to reset the variable for the next loop
isset = false;
}
你的整个函数会是这样的:
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue;
// This variable switch if the filter catch any result
var isset = false;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// This variable switch if the filter catch any result
var isset = false;
//This loop evalaute all rows
for (i = 0; i < tr.length; i++) {
//Here we search for all columns in this row
//note that we get rid of [0] at the end because we're looking for all
//tds, not just the first one
let tds = tr[i].getElementsByTagName("td");
//Here we evaluate each column for this row
for (let td of tds) {
//If td has a value
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
//If the filter catch any result, we show the row
isset = true;
}
}
}
//The decision of show or hide the row is made here because
//the loop has to evaluate all columns before decide if show or hide the entire row
if (isset) {
//If the filter catch any result, we show the row
tr[i].style.display = "";
} else {
//If the filter catch no result, we hide the row
tr[i].style.display = "none";
}
//This is to reset the variable for the next loop
isset = false;
}
}
我已经用这个脚本测试了代码
<input type="text" id="myInput" onKeyUp="myFunction()" placeholder="Search">
<table class="table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th class="text-center">Name</th>
<th class="text-center">Age</th>
<th class="text-center">Hobby</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td class="text-center">Alejandro</td>
<td>12</td>
<td>Football</td>
</tr>
<tr>
<td class="text-center">Peralta</td>
<td>12</td>
<td>Alejandro</td>
</tr>
<tr>
<td class="text-center">Casemiro</td>
<td>12</td>
<td>Soccer</td>
</tr>
<tr>
<td class="text-center">Moreno</td>
<td>12</td>
<td>Tenis</td>
</tr>
<tr>
<td class="text-center">Alejandro</td>
<td>12</td>
<td>Moreno</td>
</tr>
</tbody>
</table>
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue;
// This variable switch if the filter catch any result
var isset = false;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// This variable switch if the filter catch any result
var isset = false;
//This loop evalaute all rows
for (i = 0; i < tr.length; i++) {
//Here we search for all columns in this row
//note that we get rid of [0] at the end because we're looking for all
//tds, not just the first one
let tds = tr[i].getElementsByTagName("td");
//Here we evaluate each column for this row
for (let td of tds) {
//If td has a value
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
//If the filter catch any result, we show the row
isset = true;
}
}
}
//The decision of show or hide the row is made here because
//the loop has to evaluate all columns before decide if show or hide the entire row
if (isset) {
//If the filter catch any result, we show the row
tr[i].style.display = "";
} else {
//If the filter catch no result, we hide the row
tr[i].style.display = "none";
}
//This is to reset the variable for the next loop
isset = false;
}
}
</script>