jQuery:如何从表中选择行 [英] JQuery: How to select rows from a table
本文介绍了jQuery:如何从表中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种情况,我想根据td中的值从表中选择行
I have a scenario where I would like to select rows from a table depending upon the values in td
例如我有这样的桌子
<tr>
<td>John</td>
<td>Smith</td>
<td>Male</td>
</tr>
<tr>
<td>Andy</td>
<td>Gates</td>
<td>Male</td>
</tr>
<tr>
<td>Alice</td>
<td>Nixon</td>
<td>Female</td>
</tr>
现在,如果第一个td的值是x AND 第二个td的值是y
now I would like to select all the rows if the value of first td is x AND value of second td is y
在妈妈那里,我正在做类似的事情
At the momemnt I am doing something like this
$("tr").each(function (index) {
if ($(this).find('td:eq(0)').text().trim() == x &&
$(this).find('td:eq(1)').text().trim() == y)
...do somethin....
});
遍历每一行并进行检查.这很冗长.有没有更好的方法可以在一行中实现这一目标.我似乎无法找出带有选择器的AND运算符逻辑?
looping through each row and check. This is verbose. Is there a better way to achieve this in a single line. I can't seem to figure out AND operator logic with selectors?
等待中,
推荐答案
$("tr").each(function (index) {
if ($.trim($(this).find('td:eq(0)').text()) == x &&
$.trim($(this).find('td:eq(1)').text()) == y) {
$(this).closest('table').css('border', '1px solid red'); // this should do it
}
});
或者,使用 .filter
:
Alternatively, using .filter
:
$("tr").filter(function() {
return $.trim($(this).find('td:eq(0)').text()) == 'John' &&
$.trim($(this).find('td:eq(1)').text()) == 'Smith';
}).closest('table').find('tr').css('border', '1px solid red');
演示: http://jsfiddle.net/WhFbE/5/
这篇关于jQuery:如何从表中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文