2012-11-15 3 views
3

작은 문제가 있습니다. 확인란이 선택되어 있는지 확인하고 선택되어 있으면 그의 첫 동생에게 수업을 추가하고 싶습니다. HTML :if : check this to this

<input class="box" type="checkbox"> 
    <p class="red">Red text </p> 

    <input class="box" checked type="checkbox"> 
    <p class="red">I want this text in blue </p> 

JS : 여기

if(jQuery(".box").is(":checked")) { 
    jQuery(this).next().attr('class', 'blue');        
} 

예 : 사전에 http://jsfiddle.net/LVEqn/

감사합니다! 기존 클래스를 바꾸려면,

$(".box:checked").next().addClass('blue'); 

$(".box:checked").next().attr('class', 'blue'); 

만약을 : 당신이 체크 상자 아래의 모든 요소에 클래스 '블루'를 추가하려면

답변

6

, 이렇게 이 항목이 동적이거나 사용자가 확인하거나 선택을 취소 할 때 변경되기를 원합니다.

$(".box").change(function(){ 
    $(this).next().attr('class', $(this).is(':checked') ? 'blue' : 'red'); 
}); 

DEMONSTRATION

+0

감사합니다, 모두가 잘 작동합니다. 나는 왜 내가 이것을 발견하지 못했는지 모른다. :) – Kaherdin

1

jQuery를

$(".box:checked").next().addClass("blue"); 

CSS

.blue {color: blue !important;}