英文:
I want to search from both Name and Country values
问题
我想在“Name”和“Country”值列上使用文本过滤器进行搜索,但现在我只能在一列上进行搜索。我该如何更改tr[i].getElementsByTagName('td')[0]
的值以同时过滤这两列?
我会很高兴如果你可以帮助我。
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-html -->
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
<script>
function myFunction() {
// 声明变量
var input, filter, table, tr, td, i, txtValue;
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")[0]; // 这里是你要更改的地方
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
<!-- 结束代码片段 -->
英文:
I want to search on the Name and Country values columns with the text filter, but now I can only search on one column. How can I change the value of tr[i].getElementsByTagName('td')[0]
to filter on both columns?
I'll be happy if you can help me.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
<script>
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";
}
}
}
}
</script>
<!-- end snippet -->
答案1
得分: 1
你可以选择两个字段并要求它与任何一个匹配。
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="搜索姓名或国家..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">姓名</th>
<th style="width:40%;">国家</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>德国</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>瑞典</td>
</tr>
<tr>
<td>Island Trading</td>
<td>英国</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>德国</td>
</tr>
</table>
<script>
// 声明变量
var input, filter, table, tr, tdName, tdCountry, i, txtValueName, txtValueCountry;
input = document.getElementById("myInput");
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// 初始隐藏所有表格行
for (i = 0; i < tr.length; i++) {
if (tr[i].classList.contains('header')) {
continue;
}
tr[i].style.display = "none";
}
function myFunction() {
filter = input.value.toUpperCase();
// 遍历所有表格行,并隐藏那些不匹配搜索查询的姓名或国家列
for (i = 0; i < tr.length; i++) {
tdName = tr[i].getElementsByTagName("td")[0];
tdCountry = tr[i].getElementsByTagName("td")[1];
if (tdName && tdCountry) {
txtValueName = tdName.textContent || tdName.innerText;
txtValueCountry = tdCountry.textContent || tdCountry.innerText;
if (txtValueName.toUpperCase().indexOf(filter) > -1 || txtValueCountry.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
英文:
You can select both fields and ask it to match against either.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
<script>
// Declare variables
var input, filter, table, tr, tdName, tdCountry, i, txtValueName, txtValueCountry;
input = document.getElementById("myInput");
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
// Hide all table rows initially
for (i = 0; i < tr.length; i++) {
if (tr[i].classList.contains('header')) {
continue;
}
tr[i].style.display = "none";
}
function myFunction() {
filter = input.value.toUpperCase();
// Loop through all table rows, and hide those that don't match the search query in either the Name or Country column
for (i = 0; i < tr.length; i++) {
tdName = tr[i].getElementsByTagName("td")[0];
tdCountry = tr[i].getElementsByTagName("td")[1];
if (tdName && tdCountry) {
txtValueName = tdName.textContent || tdName.innerText;
txtValueCountry = tdCountry.textContent || tdCountry.innerText;
if (txtValueName.toUpperCase().indexOf(filter) > -1 || txtValueCountry.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
<!-- end snippet -->
答案2
得分: 1
尝试这个,
<script>
function myFunction() {
// 声明变量
var input, filter, table, tr, td, td1, i, txtValue, txtValue1;
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")[0];
td1 = tr[i].getElementsByTagName("td")[1];
if (td) {
txtValue = td.textContent || td.innerText;
txtValue1 = td1.textContent || td1.innerText;
console.log(txtValue1);
if (txtValue.toUpperCase().indexOf(filter) > -1 || txtValue1.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
注意:我已经移除了 HTML 编码字符(如 <
和 "
)并将其替换为正常的 HTML 标签和引号。
英文:
Try this,
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue,txtValue1;
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];
td1 = tr[i].getElementsByTagName("td")[1];
if (td) {
txtValue = td.textContent || td.innerText ;
txtValue1 = td1.textContent || td1.innerText ;
console.log(txtValue1);
if (txtValue.toUpperCase().indexOf(filter) > -1 || txtValue1.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
答案3
得分: 1
我采用了一个更简单的方法。
假设你想搜索 tr 元素的内容。我在文本内容中使用 includes 函数来查找筛选条件。我使用这个结果来切换 tr 上的一个类。
CSS 类将显示设置为 block,同时我还使用 CSS 将 TR 默认设置为 display:none。
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
const input = document.getElementById("myInput");
const table = document.getElementById("myTable");
const trs = table.querySelectorAll("tr:not(.header)");
function myFunction() {
const filter = input.value.toUpperCase();
trs.forEach((tr) => {
tr.classList.toggle("active", (filter != "" && tr.textContent.toUpperCase().includes(filter)))
});
}
tr:not(.header) {
display: none;
}
tr.active {
display: block;
}
英文:
I went with a simpler approach.
Assuming, you want to search the content of the tr itself. I'm looking in the text content for the filter using the includes function. I use the return of that to toggle a class on the tr.
The CSS class sets the display to block, while I also use CSS to set TR to display none by default.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const input = document.getElementById("myInput");
const table = document.getElementById("myTable");
const trs = table.querySelectorAll("tr:not(.header)");
function myFunction() {
const filter = input.value.toUpperCase();
trs.forEach((tr) => {
tr.classList.toggle("active",(filter != "" && tr.textContent.toUpperCase().includes(filter)))
});
}
<!-- language: lang-css -->
tr:not(.header){display:none}
tr.active{display:block}
<!-- language: lang-html -->
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论