2011-01-11 4 views
0
<body>           
<input type="checkbox" id="chkMain" /> 
<input type="checkbox" id="chkMain1" /> 
<input type="checkbox" id="chkMain2" /> 

<br> 
<P><input class="child" type="checkbox" id="chk1" disabled="true" /> 
<input class="child" type="checkbox" id="chk" disabled="true" /> 
<input class="child" type="checkbox" id="chk" disabled="true" /> 
<input class="child" type="checkbox" id="chk" disabled="true" /> 
<input class="child" type="checkbox" id="chk" disabled="true" /> 
<input class="child" type="checkbox" id="chk" disabled="true" /> 
<input class="child" type="checkbox" id="chk_all" disabled="true" />ALL</p></br> 


$(function(){   
    $("input[id^=chkMain]").click(function(){    
     var otherCks = $("input[id^=chkMain]").not(this); 
     if(!$(this).is(":checked")) {       
      $(".child").attr("disabled" , true);          
      otherCks.removeAttr ("disabled"); 
     }     
     else {       
      $(".child").removeAttr ("disabled");     
      otherCks.attr("disabled" , true) 
     }   
    });  
}); 
+0

여기에서 "ALL"chkbox를 클릭하면 모든 자식 checkboxex를 활성화하려고합니다. – Harshil

+0

"All"확인란이 비활성화되어 있다고 혼란 스럽습니다. 이것은 말이되지 않는 것 같습니다. – JasCav

답변

2

질문에이 문제를 언급하지 않았습니다. 또한 잘못된 HTML가있는 체크 박스 chkMain

$(function(){ 
    $("#chk_all").click(function(){ 
     $("input:checkbox").attr("disabled", !(this.checked)); 
    }); 
}); 

을 클릭 할 때 클래스 이름 자녀와 함께 모든 확인란을 선택하려는 가정. 두 개 이상의 요소가 동일한 ID를 가지고 있습니다.

+0

+1과 대답은 잘못된 HTML입니다. – JasCav

+0

oh, sry ... 1에서 5까지 번호가 매겨진 chkbox를 가정하면 모든 chkbox가 배치됩니다 !!! 위의 코드는 어디에 붙여야합니까 ?? 후 다른 후 ?? – Harshil

+0

요구 사항을 좀 더 명확하게 지정할 수 있습니까? – rahul

0

귀하의 질문은 명확하지 않습니다. 체크 박스를 활성화/비활성화하기위한 응답이 이미 있습니다. 그러나 코드에서 수집 할 수있는 내용을 기반으로 모든 체크/체크하지 않음 체크 박스를 만들고 싶습니다. 그렇다면

, 당신은 간단한 체크 모든으로 다음을 수행 할 수/취소 - 모든 :

<script type="text/javascript"> 
$(document).ready(function() { 
    $('input.check_all').click(function() { 
     if ($(this).attr('checked')) 
     { 
      $('input.child, input.check_all').attr('checked', 'checked'); 
     } 
     else 
     { 
      $('input.child, input.check_all').removeAttr('checked'); 
     } 
    }); 
}); 
</script> 

<input class="check_all" type="checkbox" name="foo[]" /> All<br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 
<input class="child" type="checkbox" name="foo[]" /><br /> 

귀하의 체크 박스는 아마 이름을 포함해야합니다. 또한 여러 DOM 요소에서 동일한 ID를 사용할 수 없으므로 오류가 발생합니다.

관련 문제