5

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

3 Answers 3

13

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");
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Up vote, thank you so much. This was driving me nuts. How did I miss this upon HTML inspection is beyond me.
Oh my gosh! You just ended a bunch of hair pulling for me. Thank you very much!
2

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

Comments

0
$(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')

1 Comment

thanks for the reply...but still it doesn't work. the alert always shows false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.