这是我的搜索代码,它在未添加通知功能时工作正常。但是,一旦我添加了#34; Notification"功能它不会使这"搜索"功能工作。请帮忙
编辑:我添加了关于我如何显示具有搜索功能的表数据的代码。
$(document).ready(function(){
$("#search").keyup(function(){
if( $(this).val() != "")
{
$("#table tbody>tr").hide();
$("#table td:contains-ci('" + $(this).val() + "')").parent("tr").show();
}
else
{
$("#table tbody>tr").show();
}
});
});
$.extend($.expr[":"],
{
"contains-ci": function(elem, i, match, array)
{
return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
然后这是其输入的Html代码
<input type="search" id="search" class = "search" name = "search" placeholder="Search...">
然后这是我在页面中填充表格的代码
<label id = "title">Manage Licenses</label><br/>
</div>
</div>
<div class="row" style = "padding-bottom : 20px;">
<?php
$qry = $con->prepare("SELECT * FROM tbllicense, tblrequests WHERE ExpirationDate < DATE_ADD(NOW(), INTERVAL 2 MONTH) AND
tblrequests.ID = Request_ID AND tblrequests.Status = 'accepted' ORDER BY ExpirationDate");
$qry->execute();
$rows = $qry->fetchAll(PDO::FETCH_ASSOC);
$expiredcount = 0;
?>
<table class = "table" id = "table">
<thead>
<th>Record ID</th>
<th>Name</th>
<th>Email</th>
<th>License Type</th>
<th>Expiration</th>
<th>Options</th>
<tbody>
<?php
foreach ($rows as $row) {
?>
<tr>
<td><?= $row["Request_ID"] ?></td>
<td><?= $row["txtFName"] . " " . $row["txtSurname"] ?></td>
<td><?= $row["txtEmail"] ?></td>
<td><?= $row["txtLicense"] ?></td>
<td><?= date("M d, Y", strtotime($row["ExpirationDate"])) ?></td>
<td><input type = "button" class = "btnAccept" value = "Renew" onclick = "renew(<?= $row["Request_ID"] ?>)"/></td>
</tr>
<?php
}
?>
</thead>
</tbody>
</table>
</div>
当我添加此功能(通知)时。搜索无法正常工作,当我在搜索表单中输入输入时,结果将为空白。
<li style = "float:right">
<div style = "float:right">
<?php
$qry = $con->prepare("SELECT * FROM tbllicense, tblrequests WHERE ExpirationDate < DATE_ADD(NOW(), INTERVAL 2 MONTH) AND
tblrequests.ID = Request_ID AND tblrequests.Status = 'accepted'
UNION
SELECT * FROM tblvisa, tblrequests WHERE ExpirationDate < DATE_ADD(NOW(), INTERVAL 2 MONTH) AND
tblrequests.ID = Request_ID AND tblrequests.Status = 'accepted' ORDER BY ExpirationDate"
);
$qry->execute();
$rows = $qry->fetchAll(PDO::FETCH_ASSOC);
if ($qry->rowCount()) {
echo '<img width=15 height=13 src="img/alert.png" />';
}
?>
</div>
<a href='#' id = "notificationLink" class = "notificationLink" title = "Notification"><img src="img/notification.png"
style = " position : relative; width : 15px; height : 13px;"/></a>
<div id="notificationContainer">
<div id="notificationTitle">Notifications</div>
<div id="notificationBody" class="scrollbar2">
<?php
$qry = $con->prepare("SELECT * FROM tbllicense, tblrequests WHERE ExpirationDate < DATE_ADD(NOW(), INTERVAL 2 MONTH) AND
tblrequests.ID = Request_ID AND tblrequests.Status = 'accepted' ORDER BY ExpirationDate");
$qry->execute();
$rows = $qry->fetchAll(PDO::FETCH_ASSOC);
$expiredcount = 0;
?>
<table class = "table" id = "table">
<thead>
<?php
foreach ($rows as $row) {
$expiredcount++;
?>
<tr>
<td><a href='_admin_business_license.php' style = "color: white;" class = "link">The Request <?= $row["Request_ID"] ?> is expired or about to expire on
<?= date("M d, Y", strtotime($row["ExpirationDate"])) ?> from the Business License</a></td>
</tr>
<?php
}
?>
<?php
$qry = $con->prepare("SELECT * FROM tblvisa, tblrequests WHERE ExpirationDate < DATE_ADD(NOW(), INTERVAL 2 MONTH) AND
tblrequests.ID = Request_ID AND tblrequests.Status = 'accepted' ORDER BY ExpirationDate");
$qry->execute();
$rows = $qry->fetchAll(PDO::FETCH_ASSOC);
$expiredcount1 = 0;
?>
<?php
foreach ($rows as $row) {
$expiredcount1++;
?>
<tr>
<td><a href='_admin_visa.php' style = "color: white" class = "link">The Request <?= $row["Request_ID"] ?> is expired or about to expire on
<?= date("M d, Y", strtotime($row["ExpirationDate"])) ?> from the Visa</a></td>
</tr>
<?php
}
?>
</thead>
</table>
</div>
<div id="notificationFooter"><a href="notification.php">See All</a></div>
</div>
</li>
答案 0 :(得分:0)
您的搜索功能正在搜索tbody,但您的代码中只包含该内容。
除非有更多的代码我们没有看到(我怀疑你关闭了表格)然后我认为这是问题
变化
$("#table tbody>tr").hide();
到
$("#table thead>tr").hide();
和
$("#table tbody>tr").show();
到
$("#table thead>tr").show();
您还需要执行以下操作
$(document).ready(function(){
$("#search").keyup(function(){
var txtVal = $(this).val();
if(txtVal != "")
{
$("#table > thead > tr").show();
$.each($("#table > thead > tr"), function (i, o) {
var match = $("td:contains-ci('" + txtVal + "')",this)
if (match.length > 0) $(this).show();
else $(this).hide();
});
}
else
{
$("#table thead > tr").show();
}
});
});
答案 1 :(得分:0)
非常感谢那些回答我问题的人:)
我发现它留下空白页面的原因是因为我正在搜索通知和管理页面的相同“表格”。
我更改了“表格”的ID,导致与搜索代码发生冲突。
$("#search").keyup(function(){
if( $(this).val() != "")
{
$("#searchtable tbody>tr").hide();
$("#searchtable td:contains-ci('" + $(this).val() + "')").parent("tr").show();
}
else
{
$("#table tbody>tr").show();
}
});
});
这是搜索必须通过
运行的表的新ID <table class = "table" id = "searchtable">
<thead>
<th>Record ID</th>
<th>Name</th>
<th>Email</th>
<th>License Type</th>
<th>Expiration</th>
<th>Options</th>
<tbody>
<?php
foreach ($rows as $row) {
?>
<tr>
<td><?= $row["Request_ID"] ?></td>
<td><?= $row["txtFName"] . " " . $row["txtSurname"] ?></td>
<td><?= $row["txtEmail"] ?></td>
<td><?= $row["txtLicense"] ?></td>
<td><?= date("M d, Y", strtotime($row["ExpirationDate"])) ?></td>
<td><input type = "button" class = "btnAccept" value = "Renew" onclick = "renew(<?= $row["Request_ID"] ?>)"/></td>
</tr>
<?php
}
?>
</thead>
</tbody>
</table>