jeudi 21 avril 2016

How can I determine whether a checkbox is checked in jQuery? [duplicate]

This question already has an answer here:

I dynamically create a bunch of checkboxes like so:

@foreach (var rpt in reports)
{
    @* convert id to lowercase and no spaces *@
    var morphedRptName = @rpt.report.Replace(" ", string.Empty).ToLower();
    <input class="leftmargin8, ckbx" id="ckbx_@(morphedRptName)" type="checkbox" value="@rpt.report" />@rpt.report
}

I've got this event handler where I want to determine their state - checked or unchecked:

$(".ckbx").change(function () {
    if ($(this).checked) {
        alert('checkbox is unchecked');
        checkboxSelected = false;
        return;
    }
    alert('checkbox is checked');
    . . .

However, the condition "if ($(this).checked)" is always false, both when I check a checkbox and when I subsequently uncheck/deselect it.

So what do I need to do to determine when it is unchecked? I actually tried this first: "if (!$(this).checked)" but that just did the reverse - the condition was always true.




Aucun commentaire:

Enregistrer un commentaire