以下是代码:
<table class="sortable doOddEven" id="queryResult">
<tbody>
<tr>
<th>Index</th>
<th>info1</th>
<th>info2</th>
<th>Date</th>
</tr>
<tr>
<th>1</th>
<td>1234</td>
<td>2</td>
<td>June 1</td>
</tr>
<tr>
<th>2</th>
<td>1235</td>
<td>3</td>
<td>June 2</td>
</tr>
<tr>
<th>3</th>
<td>1236</td>
<td>4</td>
<td>June 3</td>
</tr>
</tbody>
</table>
我的查询是:如何遍历基于jQuery的标题描述?
对于上面的代码,我需要验证“info2”标题下的值。如果是“3”,我需要打印相应的行日期。
你能帮帮我吗
提前致谢
答案 0 :(得分:0)
试试这个:
// Get the table jquery object
var $table = $('#queryResult');
// Get the index of the column having 'info2' text
var index = $table.find("th:contains('info2')").index();
// Loop through all the table rows
$table.find('tr').each(function(){
// Get the text of that column in each row
var text = $(this).find('td:eq(' + (index -1) + ')').text();
// if it is "3", alert corresponding row's date.
if(text == '3')
alert($(this).find('td:eq(' + index + ')').text()); // June 2
});