我在一个php页面中有两个搜索栏,但第二个不起作用。 我有单选按钮组:
<form>
<p><input type="radio" value="com" name="radioPM" />One</p>
<p><input type="radio" value="all" name="radioPM" />Two</p>
</form>
带有一些jQuery,显示div,具体取决于单选:
<script>
$(document).ready(function () {
$('input[type=radio][name=radioPM]').change(function () {
if (this.value == 'com') {
$("#comDiv").show();
$("#allDiv").hide();
} else if (this.value == 'all') {
$("#allDiv").show();
$("#comDiv").hide();
}
});
});
</script>
之后,我有两个div,每个div都位于自己的搜索栏中:
<div id ='comDiv' style="display:none">
<input type="text" id="myInput" onkeyup="searchByName()" placeholder="search..." title="search...">
<?php
include_once('query_1.php');
echo "<table border='1' id='myTable'>
<tr>
<th>One</th>
<th>Two</th>
</tr>";
while ($row = mysqli_fetch_array($query_1)) {
echo "<form method=\"post\"><tr>";
echo "<td>" . $row['ONE'] . "</td>";
echo "<td>" . $row['TWO'] . "</td>";
echo "</tr></form>";
}
echo "</table>";
?><br>
</div>
<div id ='allDiv' style="display:none">
<input type="text" id="myInput" onkeyup="searchByName()" placeholder="search..." title="search...">
<?php
include_once('query_2.php');
echo "<table border='1' id='myTable'>
<tr>
<th>One</th>
<th>Two</th>
</tr>";
while ($row = mysqli_fetch_array($query_2)) {
echo "<form method=\"post\"><tr>";
echo "<td>" . $row['ONE'] . "</td>";
echo "<td>" . $row['TWO'] . "</td>";
echo "</tr></form>";
}
echo "</table>";
?><br>
</div>
最后,我有了带有搜索栏功能的脚本:
<script>
function searchByName() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
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>
我的问题是,如果我选择第一个选项(value = com)一切正常,则comDiv出现,搜索栏从mysql DB中搜索表。但是,如果我选择第二个选项(value = all),comDiv隐藏,并且allDiv出现,则显示来自mysql DB的数据,但搜索栏不起作用。您能帮我吗,我做错了什么?
答案 0 :(得分:1)
id
在HTML文档中必须唯一(myInput
和myTable
两次使用)
您可以像下面那样更新代码或使用conman搜索框
<input type="text" id="myInput1" onkeyup="searchByName('myInput1','myTable1')" placeholder="search..." title="search..."> // id as searchByName parameter
<input type="text" id="myInput2" onkeyup="searchByName('myInput2','myTable2')" placeholder="search..." title="search...">
<script>
function searchByName(myInput,myTable) {
var input, filter, table, tr, td, i;
input = document.getElementById(myInput);
filter = input.value.toUpperCase();
table = document.getElementById(myTable);
tr = table.getElementsByTagName("tr");
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>
并更改表ID echo "<table border='1' id='myTable1'>
和"<table border='1' id='myTable2'>
答案 1 :(得分:0)
我认为您应该在searchByName()函数中提供特定的选择器。因为两个表都具有相同的ID“ myTable”。您应该更改表ID,也可以指定$(“#comDiv”)。find(“ myTable”)和$(“#allDiv”)。find(“ myTable”)