jQuery 检查是否选中了特定的单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2195125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether specific radio button is checked
提问by PositiveGuy
I'm having trouble after looking at the jQuery docs. I'm just trying to return true/false in one my my jquery methods depending on the check of a certain radiobutton and if it's selected or not
查看 jQuery 文档后,我遇到了麻烦。我只是想在我的 jquery 方法之一中返回真/假,具体取决于对某个单选按钮的检查以及它是否被选中
I've tried several things but have not been able to get this right:
我已经尝试了几件事,但一直无法做到这一点:
<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 in my method I have this:
在我的方法中,我有这个:
return $("input[@name='test2']:checked");
I'm getting an undefined on $("input[@name='test2']:checked");
我得到了一个未定义的 $("input[@name='test2']:checked");
UPDATED:
更新:
example:
例子:
<input type="radio" runat="server" name="radioGroup" id="payPalRadioButton" value="paypalSelected" />
this returns 'undefined' still:
这仍然返回“未定义”:
$("input[@name=radioGroup]:checked").attr('payPalRadioButton');
If I try this, I get 'false' even if I select the radio button:
如果我尝试这个,即使我选择了单选按钮,我也会得到“假”:
$('input:radio[name=radioGroup]:checked').val() == 'paypalSelected'
回答by PetersenDidIt
Your selector won't select the input field, and if it did it would return a jQuery object. Try this:
您的选择器不会选择输入字段,如果选择了它,它将返回一个 jQuery 对象。尝试这个:
$('#test2').is(':checked');
回答by ghoppe
I think you're using the wrong approach. You should set the value
attribute of your input elements. Check the docs for .val()for examples of setting and returning the .val() of input elements.
我认为您使用了错误的方法。您应该设置value
输入元素的属性。检查.val() 的文档以获取设置和返回输入元素的.val()的示例。
ie.
IE。
<input type="radio" runat="server" name="testGroup" value="test2" />
return $('input:radio[name=testGroup]:checked').val() == 'test2';
回答by naivists
1.You don't need the @
prefix for attribute names any more:
1.您不再需要@
属性名称的前缀:
http://api.jquery.com/category/selectors/attribute-selectors/:
http://api.jquery.com/category/selectors/attribute-selectors/:
Note: In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the ‘@' symbol from your selectors in order to make them work again.
注意:在 jQuery 1.3 中 [@attr] 样式选择器被删除(它们之前在 jQuery 1.2 中被弃用)。只需从选择器中删除“@”符号即可使它们再次工作。
2.Your selector queries radio buttons by name
, but that attribute is not defined in your HTML structure.
2.您的选择器通过 查询单选按钮name
,但该属性未在您的 HTML 结构中定义。
回答by C???
You should remove the '@' before 'name'; it's not needed anymore (for current jQuery versions).
您应该删除“名称”之前的“@”;不再需要它(对于当前的 jQuery 版本)。
You're want to return all checked elements with name 'test2', but you don't have any elements with that name, you're using an idof 'test2'.
您想要返回名称为“test2”的所有已检查元素,但您没有任何具有该名称的元素,您使用的id为“test2”。
If you're going to use IDs, just try:
如果您要使用 ID,请尝试:
return $('#test2').attr('checked');
回答by Marc B
WHy bother with all of the fancy selectors? If you're using those id="" attributes properly, then 'test2' must be the only tag with that id on the page, then the .checked boolean property will tell you if it's checked or not:
为什么要打扰所有花哨的选择器?如果您正确使用了那些 id="" 属性,那么 'test2' 必须是页面上唯一具有该 id 的标记,然后 .checked 布尔属性会告诉您它是否被选中:
if ($('test2').checked) {
....
}
You've also not set any values for those radio buttons, so no matter which button you select, you'll just get a blank "testGroup=" submitted to the server.
您还没有为这些单选按钮设置任何值,因此无论您选择哪个按钮,您都只会得到一个提交给服务器的空白“testGroup=”。
回答by Rakesh
$("input[@name='<%=test2.ClientID%>']:checked");
use this and here ClientID fetch random id created by .net.
使用这个和这里 ClientID 获取由 .net 创建的随机 ID。
回答by Hamza Khanzada
Just found a proper working solution for other guys,
刚刚为其他人找到了合适的工作解决方案,
// Returns true or false based on the radio button checked
$('#test1').prop('checked')
$('body').on('change','input[type="radio"]',function () {
alert('Test1 checked = ' + $('#test1').prop('checked') + '. Test2 checked = ' + $('#test2').prop('checked') + '. Test3 checked = ' + $('#test3').prop('checked'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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 in your method you can use like
在你的方法中你可以使用像
return $('#test2').prop('checked');