how to disable a button when all the checkboxes in

2020-03-31 09:02发布

问题:

I have a grid in which I have 1 coulmns has checkboxes.I want to disable a button when all the checkbox are disabled or unticked(uncheked) using JavaScript or jQuery?

.Aspx File

<asp:TemplateField HeaderText="Cancel SO Line Item">
        <ItemTemplate>
        <asp:checkbox ID="cbSOCan" runat="server" ViewStateMode="Enabled" EnableViewState="true"></asp:checkbox>
         </ItemTemplate>

     <asp:LinkButton CssClass="btn btn-primary" ID="btnCancelItem" runat="server" CausesValidation="False"OnClientClick="return Confirmationbox();">&nbsp;Cancel Item</asp:LinkButton>
 <asp:HiddenField id="hdnval" value=0 runat="server"/>

回答1:

You can do something like the following, but you'll need to update it with more specific selectors so it doesn't affect all checkboxes and buttons:

JS Fiddle

$('input[type=checkbox]').change(function(){
    var count = $('input[type=checkbox]:checked').length;
    $('button').prop('disabled', count == 0);
});

And if you need it on load as well:

JS Fiddle

$('input[type=checkbox]').change(function(){
    disableCheckbox();
});

disableCheckbox = function() {
    var count = $('input[type=checkbox]:checked').length;
    $('button').prop('disabled', count == 0);
};

disableCheckbox();