您的位置:首页 > Web前端 > JQuery

check radio button is checked use jquery

2010-11-15 21:30 295 查看
您现在是在 : 最近的话题 » 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 PMwell 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 codevar _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 PMAlternatively, to get the currently selected radio button's id:

Copy codevar id = $("input[@name=testGroup]:checked").attr('id');

Or if each input has a unique 'value' attribute:

Copy codevar value = $("input[@name=testGroup]:checked").val();
对mark.dezelon's 回复发表评论

Re: Checking if certain Radiobutton is checked

作者: Guest 在 03-Feb-2010 04:08 PMThanks. 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 PMexample:

<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 PMthey way i do it is

Copy codebFlag = $('#test2').is(':checked');
对elubin's 回复发表评论

Re: Checking if certain Radiobutton is checked

作者: dave.methvin 在 04-Feb-2010 11:30 AMYour 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') {
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: