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");
}
});
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
We can check the status of a checkbox by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') .
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");
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With