2013-01-14 2 views
0

사용자가 클릭 한 행 번호를 확인하고 경고하려고합니다. 계산할 수 없도록 표 머리글을 면제하고 싶었 기 때문에 this.rowIndex - 1을 의도적으로 사용했지만 아무 행이나 클릭해도 경고 상자가 NaN 값을 반환합니다.Clicked 테이블 행 반환 값 NaN

어떻게 해결할 수 있습니까?

<!DOCTYPE html> 
<html> 
<head> 
<style type="text/css"> 
#mstrTable { 
    border: 1px solid black 
} 
#mstrTable td, th { 
    border: 1px solid black 
} 

#mstrTable tr.normal td { 
    color: black; 
    background-color: white; 
} 
#mstrTable tr.highlighted td { 
    color: white; 
    background-color: gray; 
} 
</style> 
</head> 
<body> 
    <table id="mstrTable"> 
    <thead> 
     <tr> 
     <th>File Number</th> 
     <th>Date1</th> 
     <th>Date2</th> 
     <th>Status</th> 
     <th>Num.</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
     <td>KABC</td> 
     <td>09/12/2002</td> 
     <td>09/12/2002</td> 
     <td>Submitted</td> 
     <td>0</td> 

     </tr> 
     <tr> 
     <td>KCBS</td> 
     <td>09/11/2002</td> 
     <td>09/11/2002</td> 
     <td>Approved</td> 
     <td>1&nbsp;</td> 
     </tr> 

     <tr> 
     <td>WFLA</td> 
     <td>09/11/2002</td> 
     <td>09/11/2002</td> 
     <td>Submitted</td> 
     <td>2</td> 
     </tr> 
     <tr> 
     <td>WTSP</td> 
     <td>09/15/2002</td> 
     <td>09/15/2002</td> 
     <td>In-Progress</td> 
     <td>3</td> 
     </tr> 
    </tbody> 
    </table> 

<script type="text/javascript"> 
(
    function() 
    { 
     var trows = document.getElementById("mstrTable").rows; 

     for (var t = 1; t < trows.length; ++t) 
     { 
      trow = trows[t]; 
      trow.className = "normal"; 
      trow.onclick = highlightRow; 
     } 

     function highlightRow(e) 
     { 
     alert('Row is ' + this.rowIndex-1) 
      for (var t = 1; t < trows.length; ++t) 
      { 
       trow = trows[t]; 
       trow.className = (trow == this && trow.className != "highlighted") ? "highlighted" : "normal"; 
      } 
     } 
    } 
)(); 
</script> 
</body> 
</html> 

답변

3

수학 연산을 둘러싼 괄호를 문자열 연결보다 우선하도록하십시오.

alert('Row is ' + (this.rowIndex-1)) 

Fiddle

+1

대단히 감사합니다. 매력처럼 작동했습니다. 다음 번에 주목! – user1959234

0

변경이 this.rowIndex-1 대신 this.rowIndex합니다.

+0

그 전에 시도했지만 추가 된 parathensis는 위의 답변에 고정시켰다. 당신의 공헌에 감사드립니다. – user1959234