How to fetch the value of selected checkbox inside a checkboxList?
I want to know the selected value of the markup below. So that I can disabled a textbox, if one of the checkbox is selected.
<asp:CheckBoxList ID="ChkTest" runat="server" RepeatDirection="Horizontal" CssClass="toggleYesNo">
<asp:ListItem Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:CheckBoxList>
I tried using this function it doesnot seem to work
$(document).ready(function() {
$("#<%=ChkTest.ClientID %>").click(function() {
value = $(this).val();
if(value=='1') {
$('#atextbox').attr('disabled','');
}
else {
$('#atextbox').attr('disabled','disabled');
}
});
});
I also track the output HTML but the id the CheckBoxList the assigned to a table instead.
UPDATED
<table id="ChkTest" class="toggleYesNo" border="0">
<tr>
<td><input id="ChkTest_0" type="checkbox" name="ChkTest$0" /><label for="ChkTest_0">Yes</label></td>开发者_如何学运维;<td><input id="ChkTest_1" type="checkbox" name="ChkTest$1" /><label for="ChkTest_1">No</label></td>
</tr>
</table>
Ok, I solved it
my jQuery function is
$(document).ready(function() {
$("#<%=ChkTest.ClientID %> input").click(function() {
value = $(this).attr('checked');
if(value==true) {
$("#TxtName").removeAttr("disabled");
}
else {
$("#TxtName").val('');
$("#TxtName").attr("disabled","disabled");
}
});
});
This totally solved it
It could be the selector; can you try changing
$("#<%=ChkTest.ClientID %>").click(function() {
To:
$('.ToggleYesNo').click(function() {
If that doesn't help, can you comment with the output you get from:
alert($("#<%=ChkTest.ClientID %>"));
alert($('.ToggleYesNo'));
try
$(document).ready(function() {
$("#ChkTest").click(function() {
value = $(this).find(':checked').val();
if(value=='1') {
$('#atextbox').attr('disabled','');
}
else {
$('#atextbox').attr('disabled','disabled');
}
});
});
EDIT based on UPDATED by the OP
$(document).ready(function() {
$("#ChkTest :checkbox").click(function() {
value = $(this).next().text().toLowerCase();
if(value=='yes') {
$('#atextbox').attr('disabled','');
}
else {
$('#atextbox').attr('disabled','disabled');
}
});
});
精彩评论