搜索表javascript
如何搜索表中的列搜索表javascript,javascript,html,Javascript,Html,如何搜索表中的列随机ID,我只能搜索唯一ID列中的值。有什么错误吗 示例代码: <table> <tr><th>Unique ID</th><th>Random ID</th></tr> <tr><td>214215</td><td>442</td></tr> <tr><td&
随机ID
,我只能搜索唯一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();
}
}
});
});
唯一IDRandom ID
214215442
1252512556
21144666
3245466334
2411154364
$(“#搜索”)。在(“键控”,函数()上{
var值=$(this.val();
$(“表tr”)。每个(函数(索引){
如果(索引!==0){
$row=$(此项);
var id=$row.find(“td:first”).text();
如果(id.indexOf(值)!==0){
$row.hide();
}
否则{
$row.show();
}
}
});
});
您需要使用:第n个子项(2)
而不是:第一个
来获取第二个td,而不是第一个td:
更多信息
使用第n个子属性
$(“#搜索”)。在(“键控”,函数()上{
var值=$(this.val();
$(“表tr”)。每个(函数(索引){
如果(索引!==0){
$row=$(此项);
var id=$row.find(“td:nth-child(2)”).text();
如果(id.indexOf(值)!==0){
$row.hide();
}
否则{
$row.show();
}
}
});
});代码>
唯一IDRandom ID
214215442
1252512556
21144666
3245466334
2411154364
$("#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();
}
}
});
});