2 回答

TA贡献1864条经验 获得超2个赞
根据您的示例(obv 您将适应您的代码):
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue;
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")[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
#myInput {
background-image: url('/css/searchicon.png'); /* Add a search icon to input */
background-position: 10px 12px; /* Position the search icon */
background-repeat: no-repeat; /* Do not repeat the icon image */
width: 100%; /* Full-width */
font-size: 16px; /* Increase font-size */
padding: 12px 20px 12px 40px; /* Add some padding */
border: 1px solid #ddd; /* Add a grey border */
margin-bottom: 12px; /* Add some space below the input */
}
#myTable {
border-collapse: collapse; /* Collapse borders */
width: 100%; /* Full-width */
border: 1px solid #ddd; /* Add a grey border */
font-size: 18px; /* Increase font-size */
}
#myTable th, #myTable td {
text-align: left; /* Left-align text */
padding: 12px; /* Add padding */
}
#myTable tr {
/* Add a bottom border to all table rows */
border-bottom: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
/* Add a grey background color to the table header and on hover */
background-color: #f1f1f1;
}
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">ROW 1</th>
<th style="width:40%;">ROW 2</th>
</tr>
<tr>
<td>RESULT</td>
<td>RESULT 2</td>
</tr>
<tr>
<td>RESULT 3</td>
<td>RESULT 4</td>
</tr>
<tr>
<td>RESULT 5</td>
<td>RESULT 6</td>
</tr>
<tr>
<td>RESULT 7</td>
<td>RESULT 8</td>
</tr>
</table>
提示:如果要执行区分大小写的搜索,请删除 toUpperCase()。
提示:如果要搜索“国家”(索引 1)而不是“名称”(索引 0),请将 tr[i].getElementsByTagName('td')[0] 更改为 [1]。

TA贡献1860条经验 获得超8个赞
这是我想要的最好的代码,它将使用 PHP Array 在大约 2000 个数据中一一搜索大约 26000 个关键字。它比通过查询在 SQL 表中搜索要快得多。
<?php
$data = array(); // Array value added from mysql database table 2000 rows (columns : ID, title, description)
$keywords = array(); // Array value added from mysql database table 26000 rows (column : keyword)
if (!$data)
{
echo "<script> alert('No Data available for Search...!');</script>";
exit();
}
$MATCHID =array();
$NOTMATCHID =array();
set_time_limit(0);// 0 = NOLIMIT
for($i = 0; $i < count($data); $i++)
{
$ID = $data[$i]['ID'];
foreach($keywords as $value)
{
$pattern = "/ ".$value." /i"; //contains pattern
$dd = preg_grep($pattern, $data[$i]);
if($dd)
{
if (in_array($ID, $NOTMATCHID))
{
$NOTMATCHID = array_diff($NOTMATCHID, array($ID));
}
$MATCHID[] = $ID;
break 1;
}
else
{
if (!in_array($ID, $NOTMATCHID))
{
$NOTMATCHID[] = $ID;
}
}
}
}
echo "<pre>";
echo "****Found IDs****<BR>";
print_r($MATCHID);
echo "<BR>****NOT Found IDs****<BR>";
print_r($NOTMATCHID);
?>
添加回答
举报