2011-08-18 2 views
2

두 개의 div에 여러 개의 확인란이 있습니다. 두 div는 actions라는 div에 들어 있습니다. 별도의 div에 '모두 선택'확인란이 있습니다. 확인란을 선택하면 다른 모든 것들을 선택하려고 시도하고 '모두 선택'을 선택 해제하면 다른 것들은 선택 해제됩니다.JQuery를 사용하여 별도의 div에서 여러 확인란 선택

다음
<div class="actions" id="actions" title="Actions"> 
    <div> 
     <div><input type="checkbox" name="action" class="act" value="0" /> <?php echo $lang["actions"][0]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="1" /> <?php echo $lang["actions"][1]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="2" /> <?php echo $lang["actions"][2]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="3" /> <?php echo $lang["actions"][3]; ?></div><br /> 

    </div> 
    <div> 
     <div><input type="checkbox" name="action" class="act" value="26" /> <?php echo $lang["actions"][26]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="27" /> <?php echo $lang["actions"][27]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="28" /> <?php echo $lang["actions"][28]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="29" /> <?php echo $lang["actions"][29]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="30" /> <?php echo $lang["actions"][30]; ?></div><br /> 
     <div><input type="checkbox" name="action" class="act" value="31" /> <?php echo $lang["actions"][31]; ?></div> 
    </div> 
</div> 
<div class="parameters1"> 
    <div class="selectAll" title="Select All"><input type="checkbox" id="selectAll" name="selectall" /> Select All</div> 
</div> 

내 JQuery와있다 :

는 HTML입니다 나는 각 함수 내에서 경고를() 넣으면

$(".selectAll").click(function() { 
     var checked_status = this.checked; 
     $('#actions').find("input").each(function() { 
      $(this).attr(":checked", checked_status); 
     }); 
}); 

, 그것은 그러나, 제대로 하나 하나 체크 박스를보고있다 $ (this) .attr 비트는 실제로 체크 박스를 변경하지 않는 것 같습니다.

아이디어가 있으십니까?

답변

2

당신은 제대로 속성을 설정하지 않을 :

$(this).prop("checked", checked_status); 

jQuery를의 1.6 버전부터 ".prop()는"A에 대한하지만,이 목적을 위해 ".attr()"선호한다 부울 속성/속성은 "checked"처럼 ".attr()"함수는 여전히 작동합니다. 속성 이름이 이 아니면 앞에 ":"이 있어야하며 실제로 업데이트하려는 속성이 ":"없이 "checked"라고 불리우므로 작업이 실패하게됩니다.

또한 명시 적으로 ".each()"가 필요하지 않습니다. 다음과 같이하면됩니다.

$('#actions input').prop('checked', checked_status);