【问题标题】:Disable a button in jquery if checkboxes with a certain class value are checked如果选中具有特定类值的复选框,则禁用 jquery 中的按钮
【发布时间】:2016-10-12 15:48:11
【问题描述】:

我有通过 php 动态生成的复选框,如果所有具有相似类值的复选框都没有被选中,我想隐藏某个按钮,如果它们被选中,我想重新启用它:

生成复选框的php代码是

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

我还有一个按钮,如果没有选中所有 1 类复选框,我想禁用它,如果它们都使用 jquery 选中,则启用它

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

我试过了:

$('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");
    }
});

});

我检查了几个资源,但大多数都没有帮助 以上失败了我该怎么做

【问题讨论】:

    标签: javascript php jquery checkbox


    【解决方案1】:

    试试这个

        $(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");
            });
        });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    相关资源
    最近更新 更多