mardi 27 août 2019

Why isn't checkbox B enabled when I check checkbox A?

I have two checkboxes, A and B. They should behave according to the following rules:

  1. A should always be enabled.
  2. B should only be enabled when A is checked
  3. If A and B is checked, and A is later un-checked, B should be un-checked and disabled automatically.

I have this code so far:

HTML

<input type="checkbox" id="A" />A
<input type="checkbox" id="B" />B

jQuery

$('#A').change(function () {
    if ($(this).attr("checked")) {
        $('#B').attr('disabled', false)
    } else {
        $('#B')
            .attr('disabled', true)
            .attr('checked', false);
    }
}).change();

Fiddle here.




Aucun commentaire:

Enregistrer un commentaire