如何搜索列 random ID
在表中,我只能搜索 unique ID
中的值柱子。似乎是什么错误?
示例代码:
<table>
<tr><th>Unique ID</th><th>Random ID</th></tr>
<tr><td>214215</td><td>442</td></tr>
<tr><td>1252512</td><td>556</td></tr>
<tr><td>2114</td><td>4666</td></tr>
<tr><td>3245466</td><td>334</td></tr>
<tr><td>24111</td><td>54364</td></tr>
</table>
<br />
<input type="text" id="search" placeholder=" live search"></input>
$("#search").on("keyup", function() {
var value = $(this).val();
$("table tr").each(function(index) {
if (index !== 0) {
$row = $(this);
var id = $row.find("td:first").text();
if (id.indexOf(value) !== 0) {
$row.hide();
}
else {
$row.show();
}
}
});
});
最佳答案
您需要使用 :nth-child(2)
而不是 :first
来获取第二个 td 而不是第一个:
更多信息 here
$("#search").on("keyup", function() {
var value = $(this).val();
$("table tr").each(function(index) {
if (index !== 0) {
$row = $(this);
var id = $row.find("td:nth-child(2)").text();
if (id.indexOf(value) !== 0) {
$row.hide();
}
else {
$row.show();
}
}
});
});
关于javascript - 搜索表javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625873/