你能帮我正确地编写Jquery代码,这样我就可以根据文本输入过滤HTML表了。到目前为止,这是我的代码:
<input type="text" name="inputdata" id="inputdata" onkeyup="filter()">
<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr name="data">
<?php foreach ($products as $row) { ?>
<td><?php echo $row->id_product; ?></td>
<td><?php echo $row->name; ?></td>
<?php } ?>
</tr>
Jquery代码
<script>
function filter(){
inp = $('#inputdata').val()
$("tr").filter(function() {
return $(this).text().indexOf(inp) == -1;
}).hide();
}
</script>
所以我的问题是:
<th>
。如果我声明$('tr[name=data]').filter(function() {
搜索停止工作答案 0 :(得分:3)
这应该解决你的问题:
<script>
function filter(){
inp = $('#inputdata').val()
// This should ignore first row with th inside
$("tr:not(:has(>th))").each(function() {
if (~$(this).text().toLowerCase().indexOf( inp.toLowerCase() ) ) {
// Show the row (in case it was previously hidden)
$(this).show();
} else {
$(this).hide();
}
});
}
</script>