大家好我需要选择名称和事件以及电子邮件并在其中搜索其中一个,因为当我输入getElementsByTagName(“td”)[1]时你可以看到;我有名字的搜索,当我把getElementsByTagName(“td”)[2];我可以在事件中搜索,但我需要搜索其中的3个而不是全部3个 谢谢你的帮助:)
<!DOCTYPE html>
<html>
<body>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable" class="table table-hover table-inverse tablesorter">
<thead>
<tr class="header">
<th>
<input type="checkbox" value="All" id="selectIDs" />
</label>
</th>
<th>Name</th>
<th>Event</th>
<th>Email</th>
<th>Iban</th>
<th>UID</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<% uids.forEach(function(uid){ %>
<tr>
<td><input type="checkbox" class="checkbox" name="productIds" value="<%=uid._id%>"></td>
<td ><%= uid.name %></td>
<td ><%= uid.event %></td>
<td ><%= uid.email %></td>
<td><%= uid.iban %></td>
<td><%= uid.uid %></td>
<td><%= uid.readableDate %></td>
</tr>
<% }) %>
</tbody>
</table>
<p id="demo"></p>
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[1];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
</body>
</html>
答案 0 :(得分:1)
你可以使用你需要的3行元素的数组,如
let newArray = new Array();
if(tr[i].getElementsByTagName("td")[1]) newArray.push(tr[i].getElementsByTagName("td")[1].innerHTML.toUpperCase());
if(tr[i].getElementsByTagName("td")[2]) newArray.push(tr[i].getElementsByTagName("td")[2].innerHTML.toUpperCase());
if(tr[i].getElementsByTagName("td")[3]) newArray.push(tr[i].getElementsByTagName("td")[3].innerHTML.toUpperCase());
然后你在这个数组中搜索:
if (newArray.filter((e,i) => e.indexOf(filter) > -1).length > 0) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
或者
if (newArray.indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
如果你想检查每个字母或完整的单词。
这样更好吗?