我正在使用以下代碼行來搜索表格的行,以檢查TD是否包含我迄今爲止工作正常的searchTerm。jQuery:按列搜索包含x的TD而不是按行
有沒有一種方法,而不是按行搜索我可以在這裏按列搜索? 原因是,在我的情況下,一行可以包含多次相同的值,但不是一個列,這是我需要在這裏。
我的表有一個標準的結構與thead和tbody,我可以添加一個colgroup或類,如果需要的話。
我的JS:
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>
提前,蒂姆非常感謝。
使用'jQuery.index()'。 – raina77ow
謝謝。我將如何在這裏應用它,以便它搜索所有列? (我的表格有一組固定的13列。) – user2571510