jQuery:按列而不是按行查找包含x的TDs
我使用以下几行搜索表中的行,以检查TD是否包含我的searchTerm,到目前为止它运行良好 有没有一种方法可以代替按行搜索,而在这里按列搜索? 原因是,在我的例子中,一行可以多次包含相同的值,但不能包含我在这里需要的列 我的表有一个带有ad和tbody的标准结构,如果需要,我可以添加colgroup或class 我的JS:jQuery:按列而不是按行查找包含x的TDs,jquery,arrays,loops,each,Jquery,Arrays,Loops,Each,我使用以下几行搜索表中的行,以检查TD是否包含我的searchTerm,到目前为止它运行良好 有没有一种方法可以代替按行搜索,而在这里按列搜索? 原因是,在我的例子中,一行可以多次包含相同的值,但不能包含我在这里需要的列 我的表有一个带有ad和tbody的标准结构,如果需要,我可以添加colgroup或class 我的JS: var searchTerm = "myText" var result = new Array(); $('#myTable tbody tr').each(functi
var searchTerm = "myText"
var result = new Array();
$('#myTable tbody tr').each(function() {
result.push(+($(this).find('td:contains(' + searchTerm + ')').next('td').text()));
});
alert(result);
<table id="myTable">
<thead>
<tr>
<th class="myHeader">Cat 1</th>
<th>Vol 1</th>
<th class="myHeader">Cat 2</th>
<th>Vol 2</th>
<th class="myHeader">Cat 3</th>
<th>Vol 3</th>
//...
</tr>
</thead>
<tbody>
<tr>
<td>item1</td><td>8</td><td>item2</td><td>7</td>
</tr>
<tr>
<td>item3</td><td>5</td><td>item2</td><td>7</td>
</tr>
<tr>
<td>item2</td><td>1</td><td>item1</td><td>5</td><td>item3</td><td>3</td>
</tr>
//...
</tbody>
</table>
我的桌子:
var searchTerm = "myText"
var result = new Array();
$('#myTable tbody tr').each(function() {
result.push(+($(this).find('td:contains(' + searchTerm + ')').next('td').text()));
});
alert(result);
<table id="myTable">
<thead>
<tr>
<th class="myHeader">Cat 1</th>
<th>Vol 1</th>
<th class="myHeader">Cat 2</th>
<th>Vol 2</th>
<th class="myHeader">Cat 3</th>
<th>Vol 3</th>
//...
</tr>
</thead>
<tbody>
<tr>
<td>item1</td><td>8</td><td>item2</td><td>7</td>
</tr>
<tr>
<td>item3</td><td>5</td><td>item2</td><td>7</td>
</tr>
<tr>
<td>item2</td><td>1</td><td>item1</td><td>5</td><td>item3</td><td>3</td>
</tr>
//...
</tbody>
</table>
第一类
第一卷
第2类
第二卷
第3类
第三卷
//...
项目18项目27
项目35项目27
项目21项目15项目33
//...
提前谢谢你,蒂姆
$('#myTable tbody tr:nth-child(myNumber)').each(function() {
var tdInMyNumberColumnValue = $(this).val();
//do stuff
}
使用nth child(myNumber)
(例如:nth child(6)
)只检查所需列中的td。例如,第6列
当然,您可以指定一个类,只指定要检查的
,并使用与此类选择器相关的each。您可以在列上循环,并使用选择器读取每个单元格:
选择作为其父元素的第n个子元素的所有元素
代码:
var searchTerm=“myText”
var result=新数组();
对于(var index=0;index<$('#myTable thead th')。长度;index++){
$(“#myTable tbody td:n子项(“+index+”))。每个(函数(){
if($(this).is(':contains('+searchTerm+')))
push(+($(this.next('td').text());
});
}
警报(结果);
演示:使用
jQuery.index()
。谢谢。我如何在这里应用它,以便它搜索所有列?(我的桌子有固定的13列。)也谢谢你。非常感谢-这看起来很棒。这在IE8中不受支持,我说的对吗?如果是,是否有一个变通方法使IE8和IE9都能做到这一点?我可以用eq()来代替它吗?我必须检查它,或者你可以使用selectivzr