2009-10-29 279 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

相关问题