2014-05-19 105 views
-3

我有這樣的事情:獲取行索引jQuery中

$("#myTable td").each(function(){ 
    if($(this).html()=="") 
    {  
     rIndex1=$(this).parent().index(); //this always stays "1" 
     rIndex2=$(this).rowIndex; //this stays as "undefined" 

但他們沒有工作。我GOOGLE了它,所有的答案取決於點擊事件。在這裏,我正在旅行所有的表格,如果我發現一個空單元格,那麼我想查找這個單元格的行索引。任何方法?

編輯:

rIndex=$(this).closest("tr").index(); //returns 1 always 
rIndex=this.parentNode.rowIndex; //returns -1 always 
rIndex=$(this).parentNode.rowIndex; //returns error msg. (I'm trying everything now) 

錯誤消息:

Uncaught TypeError: undefined is not a function

+1

'$(this).rowIndex'應該是'this.parentNode.rowIndex'。 –

+0

嘗試使用closet(「tr」)而不是父項() – Runcorn

+0

或'$(this).parent()。prop('rowIndex')'。 – undefined

回答

1

緩存在$rows變量行,然後找到當前tr(又名td父)的緩存的集合中的索引。

var $rows = $("#myTable tr"); 
$("#myTable td").each(function(){ 
    if($(this).html()=="") 
    {  
     rIndex = $rows.index($(this).parent()); 
    } 
});