如何使用 jQuery 过滤表格?
我一直在尝试使用 jQuery(在咖啡脚本中)在表上实现用户可配置的过滤器。
我已经用 CSS 类注释了表中的每一行,该 CSS 类标识了其类别和品牌:
<table id="items_list">
<tr>
<th>Foo</th>
... <!-- 6 header columns -->
</tr>
<tr class="category-12 brand-37">
<td>...</td>
</tr>
<tr class="category-17 brand-4">
<td>...</td>
</tr>
我有
jQuery ->
items = $("#items-list").html() # works - though it has a <tbody> around it
$('#category_id').change -> # gets the onChange for the category dropdown
category_id = $('#category_id :selected').val() # this works, I get the id
# this next line always returns null
selected_records = $(items).filter("tr[class=category-#{category_id}]").html()
$('#items_list').html(selected_records)
倒数第二行肯定是错误的,但我不明白为什么。有什么想法吗?
I'm stuck trying to implement a user-configurable filter on a table using jQuery (in coffeescript).
I have annotated each row in my table with a CSS class that identifies its category and brand:
<table id="items_list">
<tr>
<th>Foo</th>
... <!-- 6 header columns -->
</tr>
<tr class="category-12 brand-37">
<td>...</td>
</tr>
<tr class="category-17 brand-4">
<td>...</td>
</tr>
I have <select>
dropdown listing all of the categories, so I tried to hook on to it's onChange event to filter out only the rows in the table that match that category's ID. Here's what I've got so far -- it executes every time I change the category, but the selected_records is always null.
jQuery ->
items = $("#items-list").html() # works - though it has a <tbody> around it
$('#category_id').change -> # gets the onChange for the category dropdown
category_id = $('#category_id :selected').val() # this works, I get the id
# this next line always returns null
selected_records = $(items).filter("tr[class=category-#{category_id}]").html()
$('#items_list').html(selected_records)
That second last line must be wrong, but I can't figure out why. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必获取
html
并对其进行操作。您基本上可以只显示/隐藏table
内的tr
元素。此外,您还可以使用this.val()
来获取change
事件处理程序中select
元素的选定值。试试这个。You don't have to get the
html
and manipulate with it. You can basically just show/hide thetr
elements inside thetable
. Also you can just usethis.val()
to get the selected value of aselect
element inside thechange
event handler. Try this.