简体   繁体   中英

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

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

Issue : - When checking checkbox 1 and automatically checking checkbox 2, checkbox 2 doesnt response anymore

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);
        }

    })

});

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