Checking jQuery Check for Checkbox

The function always returns false if the check box is selected. I really could not hack what I am doing wrong. I use the checkbox to enable and disable the text box in gridview. However, this does not seem to work. Thanks for the help. I 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");
        }
});
+5
source share
3 answers

ASP.NET probably does not apply the value CssClassto the checkbox itself, but to the generated label and / or container element.

Use : checkbox instead :

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

Since the Grid does not 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");
        }
    }); 
});
+2
source
$(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')

0
source

All Articles