Prince
Prince

Reputation: 165

JQuery Check for Checkbox is checked

The function always returns false, eventhough the checkbox is checked. I really couldn't crack down what i'm doing wrong. I'm using a checkbox to enable and disable the textbox in the gridview. However, it doesn't seem to work. Thanks for the help. I have posted the html and jq code below.

HTML code:

<asp:GridView ID="grdFees" runat="server" AllowPaging="false" CssClass="Grid" AutoGenerateColumns="false" EmptyDataText="No Data Found" EmptyDataRowStyle-HorizontalAlign="Center" EmptyDataRowStyle-CssClass="gridItem" TabIndex="5">
<Columns>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="center"
                                ItemStyle-HorizontalAlign="center" ItemStyle-Width="2%">
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkselect" runat="server" CssClass="checkbox" 
                                    Width="15px" Checked="false" />
                                </ItemTemplate>
                            </asp:TemplateField>

</Columns>
</asp:GridView>

Jquery code:

$(document).ready(function() 
    {
        $(".checkbox").click(function()
        {
        if ($(this).is(":checked")) 
        {
            alert("true");
        }else
        {
            alert("false");
        }
});

Upvotes: 5

Views: 19554

Answers (3)

thecodeparadox
thecodeparadox

Reputation: 87073

$(document).ready(function() {
    $("#chkselect").click(function(){
        if (this.checked) { // can also use $(this).is(':checked') as you do
            alert("true");
        } else {
            alert("false");
        }
    }); // you code miss "});" here
});

You can also use the selector

$(':input:checkbox') or $('input:checkbox')

Upvotes: 0

Qpirate
Qpirate

Reputation: 2078

As the Grid doesnt apply the class to the checkbox you can do something like this.

$(document).ready(function() {
    $(".checkbox :checkbox").click(function(){
        if (this.checked) {
            alert("true");
        } else {
            alert("false");
        }
    }); 
});

Upvotes: 2

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

ASP.NET probably doesn't apply the value of CssClass to the check box itself, but to the generated label and/or container element.

Try using the :checkbox selector instead:

$(document).ready(function() {
    $("input:checkbox").click(function() {
        if ($(this).is(":checked")) {
            alert("true");
        } else {
            alert("false");
        }
    });
});

Upvotes: 13

Related Questions