2012-10-18 4 views
0

확인란이있는 간단한 테이블이 있습니다. 확인되면 - 행의 색상을 변경해야합니다. 순간 부모 배경을 전환하는 방법?

<tr height="35"> 
    <td><input type="checkbox" ></td> 
    <td>Name</td> 
    <td>Surname</td> 
</tr> 

은 내가

jQuery('input[type="checkbox"]').click(function(){ 
    jQuery(this).parent().parent().css('background-color', 'red'); 
}); 

함께 할하지만 난 .parent().parent() 좋은 생각이 아니라고 생각 ... 또한 배경을 전환 할 수있는 방법은? 어쩌면 어떻게 든 변화하는 개미에 대한 점검이나 해고가 아닌가?

답변

2

대신 closest()를 사용할 수 있습니다

$("input[type='checkbox']").click(function(){ 
    $(this).closest("tr").css("background-color", "red"); 
}); 
0

당신이이를 만들려고한다 backgroun을 토글하다 d 확인란을 클릭합니다.

jQuery('input[type="checkbox"]').click(function(){ 
    var self = jQuery(this); 
    var parentTr = self.closest("tr"); 
    if(this.checked) 
     parentTr.css('background-color', 'red'); 
    else 
     parentTr.css('background-color', 'default-color or empty if no default color'); 
}); 

확인이 Demo

관련 문제