2011-01-11 43 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

在這裏我想啓用所有兒童checkboxex點擊「所有」chkbox – Harshil 2011-01-11 17:04:53

+0

我很困惑,你的「全部」複選框被禁用。這似乎沒有道理。 – JasCav 2011-01-11 17:09:50

回答

2

你還沒有在問題中提到你的問題。假設你要檢查所有的複選框與類名孩子點擊時複選框chkMain

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

你也有一個無效的HTML。多個元素具有相同的ID。

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,並且會導致錯誤。