2009-10-29 265 views
1

我禁用了除一個之外的所有複選框。點擊該複選框,我想啓用所有複選框。如果該複選框未被選中,則所有其他複選框應保持禁用狀態。任何人都可以幫我解決這個問題。 我曾嘗試使用點擊其他複選框使用Jquery啓用複選框

$(document).ready(function() {<br> 
    if ($('#mainCheckbox').is(':checked')) {<br> 
    $(".otherCheckbox").removeAttr("disabled"); 
    }  
}); 

但這不適用於我。

回答

5

這對我

<input type="checkbox" id="chkMain" /><br 
<input class="child" type="checkbox" id="chk1" disabled="true" /> 
<input class="child" type="checkbox" id="chk2" disabled="true" /> 
<input class="child" type="checkbox" id="chk3" disabled="true" /> 
<input class="child" type="checkbox" id="chk4" disabled="true" /> 
<input class="child" type="checkbox" id="chk5" disabled="true" /> 

$(function(){ 
    $("#chkMain").click (function() { 

    if (!$(this).is (":checked")) 
    { 
     $(".child").attr ("disabled" , true); 
    } 
    else 
    { 
     $(".child").removeAttr ("disabled"); 
    } 
    }); 
}); 

Working Demo

工作正常,如果您可以張貼HTML還那麼這將是更有幫助。

+1

非常感謝!這正是我正在尋找的... – Shruti 2009-10-30 06:34:25

1

這是工作:

if($("#mainCheckbox").is(":checked")){ 
    $("input:checkbox").not(this).removeAttr("disabled"); 
} 
else{ 
    $("input:checkbox").not(this).attr("disabled","true"); 
} 
0

這裏有一個解決方案(注意各功能):

function toogleOthersIf(aBoolean) { 
    $(".otherCheckBox").each(function() { 
     if (aBoolean) { 
      $(this).removeAttr("disabled"); 
     } else { 
      $(this).attr("disabled", "disabled"); 
     } 
    }); 
} 

$(document).ready(function(){ 
    $('#mainCheckBox').click(function(e) { 
     toogleOthersIf($('#mainCheckBox:checked').length > 0); 
    }); 

    toogleOthersIf(false); 
}); 
+0

哼,每一個都不需要.. – 2009-10-29 06:10:13

相關問題