开发者

jQuery index method

i know there is a lot of info about index() in jQuery. But my case is complicate and I need help.

<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>

What i need is index of "tr" element in current "tbody" where i've just checked the box.


If your code is in a handler, do this:

$('table input[type="checkbox"]').change( function() {
    var idx = $(this).closest('tr').index();
});

Example: http://jsfiddle.net/bA9dx/


Or if you're saying you need to index of the row from among all the rows in the table, do this:

var rows = $('table tr');

$('table input[type="checkbox"]').change( function() {
    var idx = rows.index( $(this).closest('tr') );

    alert( idx );
});

Example: http://jsfiddle.net/bA9dx/1/


you could use:

$('input').click(function(){
    var i = $(this).closest('tr').index();
    //i is the index
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜