简体   繁体   English

在javascript中启用复选框上的点击事件

[英]Enable the click event on checkbox in javascript

When I click on the 1st checkbox, then 2 checkbox is automatically checked and it can not be unchecked if 1st is checked and if 1 is not checked then 2 is checked manually and i am done this code in the javascript当我单击第一个复选框时,将自动选中 2 个复选框,如果选中第一个复选框,则无法取消选中,如果未选中第一个,则手动选中 2 个,我在 javascript 中完成此代码

if(firstCheckboxCheckd == true){
  secondCheckboxChecked.click = false;
}
else {
  secondCheckboxChecked.click = true;
}

I am not sure if I got your question right, but here is my take.我不确定我的问题是否正确,但这是我的看法。

Requirement: - When user check checkbox 1, it should automatically check checkbox 2要求: - 当用户选中复选框 1 时,应自动选中复选框 2

Issue : - When checking checkbox 1 and automatically checking checkbox 2, checkbox 2 doesnt response anymore问题:-选中复选框 1 并自动选中复选框 2 时,复选框 2 不再响应

solution below下面的解决方案

$(document).ready(function() {

    $('#Checkbox1').click(function(){
        let firstCheckbox = $('#Checkbox1');
        let secondCheckbox = $('#Checkbox2');

        if(firstCheckbox.is(":checked"))
        {
        console.log('firstCheckbox is checked');
        secondCheckbox.prop('checked', true);
        secondCheckbox.prop('disabled', true);
        }
        else 
        {
        console.log('secondCheckbox is checked');
         secondCheckbox.prop('disabled', false);
        }

    })

});

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

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