2009-11-18 1 views

답변

1

: eq (색인) 필터를 사용해야합니다.

선택하려는 열의 인덱스를 결정 한 후, 당신이 할 수있는 (의이 idx를 호출하자) :

$('#yourTableID tr').each(function(){ 
    // for each row: 
    var myField = $(this).children('td:eq('+idx+')'); 
    // do stuff with the selected field 
}); 
+0

이 단지 TD의 한 번에 선택합니다. 루프는 커다란 테이블을 위해 엄청날 수 있습니다. –

+0

동의하지, 해결책이 아닙니다. 그것에 대해 매우 비효율적 인 방법. – redsquare

6

가 첫 번째 열 얻으려면 :

$(function() { 
    var col = $("td:nth-child(1)"); 
}); 
+0

jQuery의 청결을 좋아합니다. 좋은 대답. –

2

가장 간단한을 것입니다 행의 헤더 위치 (색인)를 가져온 다음 같은 열 인덱스에있는 모든 셀의 값에 액세스하십시오. 이 예제를 기반으로

$('#table th').click(function() { 
    var th = $(this); 
    var index = $('th', th.parents('tr')).index(th); 
    var column = $('tbody td:nth-child(' + (index + 1) + ')', th.parents('table')); 
    var values = column.map(function() { 
     return $(this).text(); 
    }); 
    alert($.makeArray(values)); 
}); 

:

<table id="table"> 
    <thead> 
     <tr><th>head1</th><th>head2</th><th>head3</th></tr> 
    </thead> 
    <tbody> 
     <tr><td>cell1a</td><td>cell2a</td><td>cell3a</td></tr> 
     <tr><td>cell1b</td><td>cell2b</td><td>cell3b</td></tr> 
     <tr><td>cell1c</td><td>cell2c</td><td>cell3c</td></tr> 
    </tbody> 
</table>