简体   繁体   中英

Disable a button in jquery if checkboxes with a certain class value are checked

I have checkboxes generated dynamically through php and i would like to hide a certain button if all the checkboxes having a similar class value are not checked and reenable it if they are checked:

The php code that generates the checkboxes is

<table class="table table-striped">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Description</th>
                    <th>Status</th>
                    <th>Comment</th>
                </tr>
                </thead>
                <tbody>

 foreach ($checks as $m => $check) {
    $item ="";
    $checkbox ="";
   $textinput ="";
   $displayx="";

if ($check->mandatory_customer == 1) { //mandatory customer checks
 $displayx .="<i style='color:red;'>*</i>";
  $item .=  $check->item.$displayx;
   $checkbox .='<input type="checkbox" class="1" id="'.$m.'"' ;
   $textinput .='<input type="text" class="1" id="'.$m.'"' ;

     } else { //not mandatory customer
    $item .=  $check->item;
   $checkbox .='<input type="checkbox" class="0" id="'.$m.'"' ;
 $textinput .='<input type="text" class="0" id="'.$m.'"' ;

    }

echo "<tr id='" . $m . "'>";
echo "<td>" . $m . "</td>";
echo "<td>" . $item . "</td>";
echo "<td>".$checkbox."</td>";
echo "<td>".$textinput."</td>";
echo "</tr>";
}
  ?>
     }

</tbody>

I also have a button that i would like to disable if all checkboxes with class 1 are not checked and enable it if they are all checked using jquery

            <button class="btn btn-success" id="approve_btn">Approve</button>

I have tried:

$('tbody tr').each(function () {
    if ($(this).attr('class:1').find('.myCheckBox').prop('checked')) {
        doEnableButton = true;
    }
    if (!doEnableButton) {
        $('#approve_btn').prop('disabled', 'disabled')
    }
    else {
        $('#approve_btn').removeAttr("disabled");
    }
});

});

I have checked on several resources but most of them are not helpful The above fails how do i go about this

Try this

    $(document).ready(function () {
        $(":checkbox.1").on("change", function () {
            if ($(":checkbox.1").not(":checked").length > 0)
                $("#approve_btn").prop("disabled", "disabled");
            else
                $("#approve_btn").removeAttr("disabled");
        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM