2011-07-14 1 views
0

jQueryのindex()に関する情報がたくさんあることは知っています。しかし、私の場合は複雑で、私は助けが必要です。jQueryのインデックスメソッド

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

私が必要とするのは、現在の "tbody"の "tr"要素のインデックスです。あなたのコードがハンドラである場合

答えて

5

、次の操作を行います。

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

例:http://jsfiddle.net/bA9dx/


それとも、あなたがの中から行のインデックスに必要な言っている場合すべてテーブルの行は、次のようにしてください。

var rows = $('table tr'); 

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

    alert(idx); 
}); 

例:あなたが使用することができhttp://jsfiddle.net/bA9dx/1/

0

$('input').click(function(){ 
    var i = $(this).closest('tr').index(); 
    //i is the index 
}); 
関連する問題