简体   繁体   English

在 javascript 中选中复选框时禁用和或启用按钮

[英]disable and or enable button when checkbox checked in javascript

how to disable or enable button when checkbox checked.选中复选框时如何禁用或启用按钮。 this checkbox is in the loop of data so thereis many checkox, i get the checkbox using className此复选框在数据循环中,因此有很多复选框,我使用 className 获取复选框

<input type="checkbox"  name='checked[]' class='check' onclick="aktifkan()"  id='checked'  value="<?= $data['gedung'];  ?>"> <!-- [] di pilih[] untuk menampung multiple checkbox -->

button will enable if checkbox if checked and will disable when checkbox uncheck如果选中复选框,按钮将启用,取消选中复选框时将禁用

<button class="btn btn-succes btn-sm pull-right mb-2" disabled id="edit" onclick='edit()'>Edit</button>
<button class="btn btn-danger btn-sm pull-right mb-2" disabled id="hapus" onclick='hapus()'>delet</button>         

const cekbox     = document.getElementsByClassName('check');
const editt      = document.getElementById('edit');
const hapuss     = document.getElementById('hapus');
function aktifkan (){
    var i ;
    var j = cekbox.length;
    for( i = 0 ; i<= j ; i++){
        // if checkbox.checked = true; theb button.disabled = false
        if (cekbox[i].checked == true){
            editt.disabled = false;
            hapuss.disabled = false ;
        }
        // if checkbox.checke = false; then button.disabled = true
        else{
            editt.disabled = true;
            hapuss.disabled = true;
        }

    }            

}

Here is the thing you want:这是你想要的东西:

 const cekbox = document.getElementsByClassName('check'); const editt = document.getElementById('edit'); const hapuss = document.getElementById('hapus'); function aktifkan() { let j = cekbox.length; let found = false; for (let i = 0; (i < j) &&;(found). i++) { // if checkbox;checked = true. if (cekbox.item(i);checked == true) { found = true. } } if (found) { // checkbox is checked editt;disabled = false. hapuss;disabled = false. } else { // checkbox is not checked. editt;disabled = true. hapuss;disabled = true; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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