2010-06-20 3 views
1

버튼을 선택할 때 라디오 버튼이 포함 된 div의 bg 색상을 변경해야합니다. 나는 jquery를 사용하고있다. 그래서 이것을 달성 할 수있는 간단한 방법이 있어야한다.라디오 버튼을 선택한 경우 bg 색상 변경

<div class="sales_box"> 
<table> 
<tr> 
<td class="radio_cell"><input type="radio"></td> 
</tr> 
</table> 
</div> 

<div class="sales_box"> 
<table> 
<tr> 
<td class="radio_cell"><input type="radio"></td> 
</tr> 
</table> 
</div> 

답변

6

그냥 jQuery를의 closest() 방법을 사용 후 라디오 버튼의 변경 이벤트를 처리하고 : :이처럼 내 HTML 설정 뭔가가 redsquare이 말한대로

$(document).ready(function(){ 
    $('input:radio').change(function(){ 
     $(this).closest('div').css('background-color', 'red'); 
    }); 
}); 

그리고, 당신은 'shouldn 정말 인라인 CSS를 설정하지 마십시오. toggleclass 기능을 사용해야합니다. 방금 CSS 함수를 예제로 사용했습니다.

+0

이렇게하면 td 배경이 변경됩니다. – redsquare

+0

@redsquare - downvote가 필요하지 않으므로 먼저 수정해야합니다. – GenericTypeTea

+0

나는 -1하지 않았다 – redsquare

0

내가 아니라 인라인 CSS를 설정보다 toggleClass을 사용

$(function() { 
     $(".radio_cell input[type=radio]").bind("click", function() { 
      $(this).parents('div:first').css("background-color", "Blue"); 
     }); 
    }); 
+1

가장 가까운 것이 가장 좋습니다.이 방법으로 모든 부모가 필터를 가져옵니다. – redsquare

0

을보십시오.

$(document).ready(function(){ 
    $('input:radio').change(function(){ 
     $(this).closest('div').toggleClass('highlight'); 
    }); 
}); 
+0

선택 취소 동작과 관련하여 다음을 추가했습니다. http://stackoverflow.com/questions/3078781/change-bg-on-radio-deselect – Thomas

관련 문제