Warning if checkbox is unchecked and button is disabled if all checkboxes are disabled in grid on Jquery page load

how to make an alert if a checkbox is not checked and a button is disabled if all checkboxes are disabled in the grid on page load using jQuery

tried to

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

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

    disableCheckbox();
});

      

                             

<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"/>

      

+3


source to share


2 answers


You are missing # to disable the button



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

  disableCheckbox = function () {
   var count = $('input[type=checkbox]:checked').length;
   if (count == 0) { alert("nothing selected") } 
   $('#btnCancelItem').prop('disabled', count == 0);
  };

  disableCheckbox();
});

      

0


source


  disableCheckbox = function () {
                    //checked check-boxes length
                    checkedCount = $('#CP_Main_gvPOItems input[type=checkbox]:checked').length;

                    //check-boxes length
                    checkboxCount = $('#CP_Main_gvPOItems input[type=checkbox]').length;

                    //if no check-box is selected then alert
                    //                if (checkedCount == 0) {
                    //                    alert('No check-box is selected');
                    // }
                    //check for all disabled check-boxes
                    //var disableCheckBox = 0;
                    $('#CP_Main_gvPOItems input[type=checkbox]').each(function () {
                        if ($(this).is(':disabled')) {
                            disableCheckBox++;
                        }
                    });

                    //if all check-boxes are disabled then disable button
                    if (checkboxCount == disableCheckBox) {

                        $('#CP_Main_btnCancelItem').attr("disabled", "disabled"); //# is missing
                    }
                };




                disableCheckbox();

                $("#CP_Main_btnCancelItem").button().click(function () {

                    var checkedCount = $('#CP_Main_gvPOItems input[type=checkbox]:checked').length;

                    var isDisabled = $(checkedCount).is(':disabled');
                    // alert(isDisabled);
                    var status = false;
                    if (checkedCount == 0 && isDisabled) {
                        alert('No check-box is selected');
                        status = false;
                    }
                    else if (!isDisabled && checkedCount > 0) {
                        alert('Are you sure you want to cancel selected Record(s)?');
                        status = true;
                    }
                    return status;
                });

      



0


source







All Articles