繁体   English   中英

我如何在jQuery中选择多个arry?

[英]How i can select multiple arry in jquery?

嗨,大家好,我需要选择名称和事件以及电子邮件,并在其中进行搜索,而不仅是其中之一,正如我输入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>

您是否可以按需要使用具有按行排列的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";
}

如果要检查每个字母或完整的单词。

这是否更好 ?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM