我知道jQuery中有很多关于index()的信息.但我的情况很复杂,我需要帮助.
<table>
<tbody>
<tr>
<td>abc</td>
<td><input type="checkbox"/></td>
</tr>
<tr>
<td>def</td>
<td><input type="checkbox"/></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>ghi</td>
<td><input type="checkbox"/></td>
</tr>
<tr>
<td>jkl</td>
<td><input type="checkbox"/></td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我需要的是当前"tbody"中"tr"元素的索引,我刚刚选中了该框.
如果您的代码在处理程序中,请执行以下操作:
$('table input[type="checkbox"]').change( function() {
var idx = $(this).closest('tr').index();
});
Run Code Online (Sandbox Code Playgroud)
示例: http ://jsfiddle.net/bA9dx/
或者,如果您说您需要从表中的所有行中索引该行,请执行以下操作:
var rows = $('table tr');
$('table input[type="checkbox"]').change( function() {
var idx = rows.index( $(this).closest('tr') );
alert( idx );
});
Run Code Online (Sandbox Code Playgroud)
示例: http ://jsfiddle.net/bA9dx/1/