check radio button is checked use jquery
您现在是在 : 最近的话题 » Using jQuery » Checking if certain Radiobutton is checked
Checking if certain Radiobutton is checked
作者:
dschinkel
在 03-Feb-2010 03:46 PM.
在 Using jQuery
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>
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个用户有相同的咨询
Re: Checking if certain Radiobutton is checked
作者:
rwhitbeck
在 03-Feb-2010 03:51 PM
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:
Copy code
var _test2 = $("#test2"); return (_test2.attr("checked") != "undefined" && _test2.attr("checked") == "checked");对rwhitbeck's 回复发表评论
Re: Checking if certain Radiobutton is checked
作者:
mark.dezelon
在 03-Feb-2010 04:01 PM
Alternatively, to get the currently selected radio button's id:
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();对mark.dezelon's 回复发表评论
Re: Checking if certain Radiobutton is checked
作者:
Guest
在 03-Feb-2010 04:08 PM
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";
对Guest's 回复发表评论
Re: Checking if certain Radiobutton is checked
作者:
dschinkel
在 03-Feb-2010 04:10 PM
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')
对dschinkel's 回复发表评论
Re: Checking if certain Radiobutton is checked
作者:
elubin
在 03-Feb-2010 08:58 PM
they way i do it is
Copy code
bFlag = $('#test2').is(':checked');对elubin's 回复发表评论
Re: Checking if certain Radiobutton is checked
作者:
dave.methvin
在 04-Feb-2010 11:30 AM
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.
对dave.methvin's 回复发表评论
Re: Checking if certain Radiobutton is checked
作者:
arviman
在 02-Nov-2010 08:58 PM
if ( $ ( '#test2:checked' ). val () == 'true' ) {
}
查看更多关于check radio button is checked use jquery的详细内容...