2016-03-07 67 views
-1

我有一個嵌套表內的表的子行這樣如何獲得該行的索引是另一個表

<table class="top" border="1"> 
    <tr> 
     <td>1</td> 
     <td>2</td> 
    </tr> 
    <tr> 
     <td>3</td> 
     <td>4</td> 
    </tr> 
    <tr> 
     <td>5</td> 
     <td> 
      <table class="nested" border="1"> 
       <tr> 
        <td><input type="text" class="ii"></td> 
        <td>2</td> 
       </tr> 
       <tr> 
        <td>3</td> 
        <td>4</td> 
       </tr> 
       <tr> 
        <td>5</td> 
        <td>4</td> 
       </tr> 
      </table> 
     </td> 
    </tr> 
</table> 

我需要聚焦的文本框的索引。我用

1. $(this).closest("tr").index(); 

這總是返回父行的索引。

2.$(".ii").focus(function(){ 
     var tt = cells.closest("tr").index(); 
     console.log(tt); 
    }); 

返回-1。

如何獲取內錶行的索引。如何做到這一點?有人可以幫幫我嗎?在此先感謝..

+1

可以ü請簡要解釋一下? –

+0

我需要獲取焦點文本框的索引。通常我使用nearest()來獲取一行的索引。但現在我有一個內部表,我無法獲得具有文本框的內部表格行的索引。 @SundarNivash – Anu

回答

1

您也可以找到tr的,通過穿越到其父table持有textbox索引。

代碼片段:

$(".ii").focus(function() { 
    console.log($(this).closest("table.nested") // `tr`'s parent table 
    .find("tr") //Total `tr` that are available in that table 
    .index($(this).closest("tr"))); //Here you can get the index 
}); 

Fiddle Demo

+0

它的工作很棒......謝謝。約翰R – Anu

相關問題