开发者

Enable checkbox on click of other checkbox using Jquery

i have disabled all the checkboxes except one. On click of that checkbox i want to enable all the checkbox. and if that checkbox is unchecked then all other checkboxes should remain disabled. Could anybody please help me with this. I have tried to use

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

But this isn't开发者_运维知识库 working for me.


This works fine for me

<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

If you can post the HTML also then it would be more helpful.


this is working:

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


Here's a solution (note the each function):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜