您现在是在 : 最近的话题 » Using jQuery » Checking if certain Radiobutton is checked
I've got the following code:
<input type="radio" runat="server" name="testGroup" id="test1" /><label for="<%=test1.ClientID %>" style="cursor:hand" runat="server">Test1</label>
<input type="radio" runat="server" name="testGroup" id="test2" /><label for="<%=test2.ClientID %>" style="cursor:hand" runat="server">Test2</label>
<input type="radio" runat="server" name="testGroup" id="test3" /> <label for="<%=test3.ClientID %>" style="cursor:hand">Test3</label>
<input type="radio" runat="server" name="testGroup" id="test2" /><label for="<%=test2.ClientID %>" style="cursor:hand" runat="server">Test2</label>
<input type="radio" runat="server" name="testGroup" id="test3" /> <label for="<%=test3.ClientID %>" style="cursor:hand">Test3</label>
And then this to check if the 2nd radio button is checked:
return
$('#test2').attr('checked');It's returning 'undefined' and I'm not sure why.状态
已回答
2个用户有相同的咨询
well when the checkbox isn't checked there isn't a attribute called checked.
when it is checked there will be a checked="checked" attribute.
Your test should be:
when it is checked there will be a checked="checked" attribute.
Your test should be:
Copy code
- var _test2 = $("#test2");
- return (_test2.attr("checked") != "undefined" && _test2.attr("checked") == "checked");
Alternatively, to get the currently selected radio button's id:
Or if each input has a unique 'value' attribute:
Copy code
- var id = $("input[@name=testGroup]:checked").attr('id');
Or if each input has a unique 'value' attribute:
Copy code
- var value = $("input[@name=testGroup]:checked").val();
Thanks. I'll try this out again. But won't the following give me the value attribute's value? <input value="valueOfInput ...>:
$("input[@name=testGroup]:checked").val();
if that's the case then I'd have to do something like this?
return $("input[@name=testGroup]:checked").val() == "valueOfInput";
$("input[@name=testGroup]:checked").val();
if that's the case then I'd have to do something like this?
return $("input[@name=testGroup]:checked").val() == "valueOfInput";
example:
<input type="radio" runat="server" name="radioGroup" id="payPalRadioButton" value="paypalSelected" />
this returns 'undefined' still:
$("input[@name=radioGroup]:checked").attr('payPalRadioButton');
and this returns false no matter if I HAVE selected that certain radio button above:
alert($('input:radio[name=payTypeGroup]:checked').val() == 'paypalSelected')
they way i do it is
Copy code
- bFlag = $('#test2').is(':checked');
Your example looks like .NET code. Isn't the id value in the control only valid for the server side of the control? That's why you're using test2.ClientID in the label, so the client-side HTML will use the right id. You'd need to do the same for your jQuery selectors as well.
if ($('#test2:checked').val() == 'true') {
}