[英]Filter table data with Jquery based on HTML input
您能帮我正确编码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() {
搜索停止工作 提前感谢您的帮助!
这应该解决您的问题:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.