2011-11-30 3 views
4

이것은 내 테이블이며, tr 및 td를 사용합니다.jQuery 다중 선택 : nth-child()

NAME Address CITY STATE 
ABC 123 A  CA 
AB8 123 B  CA 
AFC 456 B  TX 
POI 985 C  KJ 

문서 준비가 완료되면 문서가 모두 숨겨집니다.

가 지금은 라인이 모두 그럴 유무 보여주고 싶은 : -> 칼럼 (4) = CA "및"열 (3) = B

나는 피곤 내 코드 :

$("table[id=maintablex] tr td:nth-child(4):contains('CA'), table[id=maintablex] tr td:nth-child(3):contains('B')").closest('tr').show(); 

을하지만, 그것은 모든 것을 보여줍니다 (4) = CA, (3) = B ... 내 코드는 "OR"이었습니다.

추가 FULL HTML 코드 :

<table id="table"> 
    <tr> 
     <td>ABC</td> 
     <td>123</td> 
     <td>A</td> 
     <td>CA</td> 
    </tr> 
    <tr> 
     <td>ABC</td> 
     <td>1234</td> 
     <td>B</td> 
     <td>CA</td> 
    </tr> 
    <tr> 
     <td>AUF</td> 
     <td>123</td> 
     <td>C</td> 
     <td>TX</td> 
    </tr> 
    <tr> 
     <td>ABC</td> 
     <td>456</td> 
     <td>B</td> 
     <td>TX</td> 
    </tr> 
</table> 
<script language="Javascript"> 
$("table[id=table] tr").hide(); 

// Code show here 


</script> 

내가 보여주고 싶은 그 결과는 다음과 같습니다

$("table[id=maintablex] tr td:nth-child(3):contains('B')", 
    $("table[id=maintablex] tr td:nth-child(4):contains('CA')") 
).closest('tr').show(); 

I :

AB8 123 B  CA 
+1

'table [id = maintablex]'를'# maintablex'로 바꾸는 것이 좋습니다. ** 많이 ** 빠릅니다. 다음은 증명할 jsperf입니다. http://jsperf.com/jquery-id-vs-id-attribute – Jasper

+0

예, #table을 추가하면 빠르게 실행됩니다. –

답변

1

가 왜 이런 식으로하지 이것이 더 빠르지는 모르지만 @Jasper의 응답을 기반으로하면 HY이 작업을 수행하지 : 여기

//select the table, find all `<td>` elements that contain `CA` and iterate through each of them 
$('#table') 
    .find('td:nth-child(4):contains("CA")') 
    .closest('tr') 
    .find('td:nth-child(3):contains("B")') 
    .closest('tr') 
    .addClass('active'); 

는 jsfiddle입니다 : http://jsfiddle.net/KQMXe/

+0

작동하지 않습니다. u는 다시 시도 할 수있다, 나는 다만 가득 차있는 html 부호를 다시 배치한다. –

+0

네, 이제 작동합니다. 그러나 나는 그것을 빨리 알지 못합니다. 그러나 나는 그것을 좋아한다. 나는 정말로 Loop $을 좋아하지 않는다. –

0

JS--

//select the table, find all `<td>` elements that contain `CA` and iterate through each of them 
$('#maintablex').find('td:nth-child(4):contains("CA")').each(function (index, value) { 

    //un-hide the parent `<tr>` element of this `<td>` by adding the `active` class to the `<tr>` 
    $(this).parents('tr:first').addClass('active'); 
}); 

CSS--

/*Hide all the table rows by default*/ 
tr { 
    display : none; 
} 

/*declare a class that shows the table rows when added to them*/ 
tr.active { 
    display : table-row; 
} 

UPDATE

위의 코드를 업데이트하여 각 행의 세 번째 열만 찾습니다 (이전에 그 부분을 생략했습니다). 귀하의 첫 번째 선택은 항상 주 = CA 어디가 주 = B. 나는 두 조각으로이 휴식 할 행과 일치하는 것입니다 http://jsfiddle.net/jasper/Mp7Jq/3/

+0

ty Bro. 그러나 테이블은 500 - 1000 개의 행과 같을 것이다. 따라서, .each() 사용은 여전히 ​​빠릅니까? 루프마다 느려지므로 jQuery로 1을 선택하려고합니다. –

1

:

는 여기 jsfiddle입니다. ...이 코드를 테스트하지 않았하지만 당신이 가까이해야

var stateRows = ("#maintablex tr td:nth-child(4):contains('CA')").parent(); 
var matchRows = stateRows.find("td:nth-child(3):contains('B')").parent(); 

matchRows.doWhateverYouLikeWithTheResults(); 
+0

파이어 버그에서 테스트하기 쉬운 깔끔한 코드. 나는 따옴표 만 고쳐야했다. –

0

이 도움이

$.each($('table#table tr td:nth-child(4):contains("CA")').parent(),function(){ 
    if(($.inArray(this, $('table#table tr td:nth-child(3):contains("B")').parent())) > -1){ 
     $(this).show(); 
    } 
}); 

희망을 봅니다. :)