2014-05-19 2 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" 

그러나 아무도는 작동하지 않습니다. 나는 그것을 봤 거든 대답에 대한 모든 것은 클릭 이벤트에 달려있다. 여기에서 모든 테이블을 여행 중이며 NULL 인 셀을 찾으면이 셀의 행 인덱스를 찾고 싶습니다. 어떤 방법?

편집 :

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

parent() – Runcorn

+0

대신 closet ("tr")을 사용하거나'$ (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()); 
    } 
});