jeudi 20 février 2020

How do I determine if a checkbox is checked to do something, then unchecked do something else?

I have a PHP array that populates the IDs of checkboxes in a form, then used jQuery to capture those IDs. Now I am having a hard time writing an If/Else statement to determine if the checkbox is checked, and if so do one thing, and if it's unchecked do something else. This code works outside of the If statement, but as soon as I add any of the different methods to determine if it's checked or not, it stops working.

The different methods I have used are: .click, .is(':checked'), .prop, and (this.checked) but nothing works.

This (and .change) works:

$('#' + product_array[i]).click(function(){

     alert(product_array[i]);

});

This (and all of the other methods mentioned above) does not:

$('#' + product_array[i]).click(function(){

    if(this.checked){
        alert('checked');
    }
    else {
        alert('unchecked');
    }
});



Aucun commentaire:

Enregistrer un commentaire