在jQuery过滤表中过滤多个列

时间:2017-08-17 01:45:19

标签: jquery html search indexing filter

我的代码有问题。在用于过滤掉破折号的额外功能之前,我通过所有3列搜索了表格。现在我只能搜索一个。我看了很多,发现了很多例子,但不能将它应用到我的代码中。如果有人可以帮我搜索所有3列而不是只搜索索引为[0]的第一列。我想用这种方法进行过滤。我只是不知道如何在我自己的代码中实现。如果输入电话号码或姓名或ID,则应找到它。

my @Splitversion = split(/\./,$Buildno);
my $NEXTBUILDLETTER = ++$Splitversion[3];
$NextBuild = "$Splitversion[0]\.$Splitversion[1]\.$Splitversion[2]\.$NEXTBUILDLETTER";  
print $NextBuild;

1 个答案:

答案 0 :(得分:0)

这不起作用,因为您只测试索引0.您还应该获得索引1和2.这可能不是尝试它的最佳解决方案。它有效。

<!DOCTYPE html>    
<html>   
<head>    
<style>    
* {    
  box-sizing: border-box;    
}

#myInput {    
  background-image: url('/css/searchicon.png');    
  background-position: 10px 10px;    
  background-repeat: no-repeat;    
  width: 100%;    
  font-size: 16px;    
  padding: 12px 20px 12px 40px;    
  border: 1px solid #ddd;    
  margin-bottom: 12px;    
}

#myTable {    
  border-collapse: collapse;    
  width: 100%;    
  border: 1px solid #ddd;    
  font-size: 18px;    
}

#myTable th, #myTable td {    
  text-align: left;    
  padding: 12px;    
}

#myTable tr {    
  border-bottom: 1px solid #ddd;    
}

#myTable tr.header, #myTable tr:hover {    
  background-color: #f1f1f1;    
}    
</style>

</head>    
<body>        

<h2>Number search</h2>    

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

<table id="myTable">    
  <tr class="header">    
    <th style="width:60%;">Number</th>
    <th style="width:60%;">Name</th>
    <th style="width:60%;">ID</th>    
  </tr>    
  <tr>    
    <td>905-373-3333</td>
    <td>Mike</td>
    <td>4563</td>    
  </tr>    
  <tr>    
     <td>905-333-3333</td>
    <td>adam</td>
    <td>8963</td>    
  </tr>    
  <tr>    
    <td>416-373-3432</td>
    <td>Jim</td>
    <td>9363</td>    
  </tr>    
</table>        

<script>    
function myFunction() {    
  var input, filter, table, tr, td, td1, td2, i, cleanedFilter;    
  input = document.getElementById("myInput");    
  filter = input.value.toUpperCase();    
  table = document.getElementById("myTable");    
  tr = table.getElementsByTagName("tr");          

  cleanedFilter = filter.replace("-","");          

  for (i = 0; i < tr.length; i++) {    
    td = tr[i].getElementsByTagName("td")[0];
    td1 = tr[i].getElementsByTagName("td")[1];
    td2 = tr[i].getElementsByTagName("td")[2];

    if (td) {    
      cellContent = td.innerHTML.toUpperCase().replace(/-/g,"");            
      cellContent1 = td1.innerHTML.toUpperCase().replace(/-/g,"");            
      cellContent2 = td2.innerHTML.toUpperCase().replace(/-/g,"");            

      if (cellContent.indexOf(cleanedFilter) > -1 || cellContent1.indexOf(cleanedFilter) > -1 || cellContent2.indexOf(cleanedFilter) > -1 ) {    
        tr[i].style.display = "";    
      } else {    
        tr[i].style.display = "none";    
      }    
    }          
  }    
}    
</script>        

</body>    
</html>