检查特定单选按钮是否被选中

36

阅读了jQuery文档后,我遇到了一些问题。我只是想在我的jQuery方法中检查某个单选按钮是否已选择,并根据情况返回true/false。

我尝试了几种方法,但都没有成功:

<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>

我的方法中有这样的代码:

return $("input[@name='test2']:checked");

我在 $("input[@name='test2']:checked"); 上遇到了 undefined 的问题。

更新:

示例:

<input type="radio" runat="server" name="radioGroup"  id="payPalRadioButton" value="paypalSelected" /> 

这仍会返回 'undefined':

$("input[@name=radioGroup]:checked").attr('payPalRadioButton'); 

如果我尝试这样做,即使我选择了单选按钮,我也会得到“false”:

$('input:radio[name=radioGroup]:checked').val() == 'paypalSelected'
7个回答

72

你的选择器无法选择输入框,即使它能够选择,它也会返回一个jQuery对象。试试这个:

$('#test2').is(':checked'); 

每次我选择test2时,它都返回false。 - PositiveGuy
亲爱的上帝,.NET忘记了它为ID创建的唯一随机客户端ID。 - PositiveGuy

6

我认为你使用的方法不正确。你应该设置输入元素的value属性。查看.val()文档中关于设置和返回输入元素值的示例。

例如:

<input type="radio" runat="server" name="testGroup" value="test2" />

return $('input:radio[name=testGroup]:checked').val() == 'test2';

1
好吧,我现在不能去改变所有这些。那需要对我们当前页面进行大量重构。 - PositiveGuy
正则表达式大获全胜!只需用值替换您的ID即可。关键是,输入元素(特别是复选框和单选按钮)应该有一个值。更多信息:http://www.w3.org/TR/html401/interact/forms.html#h-17.4 - ghoppe
$('input:radio[name=bar]:checked').val(); 所以我们在这里没有值,我想默认值分别为0、1和2。 - PositiveGuy
可能是这样,但我不确定你能否依赖它。请参阅我链接的w3.org文档-- value是可选的,除非类型属性的值为“radio”或“checkbox”。 - ghoppe
当我选择按钮时,按照你的例子,我仍然得到假。 - PositiveGuy

4

1.现在不再需要在属性名称前加上@前缀:

http://api.jquery.com/category/selectors/attribute-selectors/

注意:在jQuery 1.3中,已删除[@attr]样式选择器(它们先前在jQuery 1.2中被弃用)。只需从选择器中删除“@”符号即可使其重新工作。

2.您的选择器通过name查询单选按钮,但该属性未在您的HTML结构中定义。


在这种情况下,三个单选按钮的名称将相同,因为它们是一组。 - PositiveGuy
确切地说,它是相同的,但你仍然需要它在那里。 - naivists
我有名字...所以不确定问题是什么?? :( - PositiveGuy
问题是 name='test2' -- 你的单选按钮组名称实际上是 testGroup - ghoppe

0
$("input[@name='<%=test2.ClientID%>']:checked");

使用此代码,可以获取由.NET创建的随机ID,并在此处使用ClientID。


问题是6年前提出的..并且他/她也得到了答案..你不能说使用这个 - Dhara

0

你应该在'name'之前去掉'@',它不再需要(对于当前的jQuery版本)。

你想要返回所有带有名称'test2'的已选中元素,但是你没有任何具有该名称的元素,你使用的是一个'id'为'test2'。

如果你打算使用ID,只需尝试:

return $('#test2').attr('checked');

1
我仍然得到一个未定义的错误..它不知道#test2是什么。 - PositiveGuy

0

何必费尽心思使用所有花哨的选择器呢?如果您正确地使用了那些id = ""属性,那么“test2”必须是页面上唯一具有该id的标签,然后.checked布尔属性将告诉您它是否被选中:

if ($('test2').checked) {
    ....
}

你还没有为这些单选按钮设置任何值,因此无论选择哪个按钮,都只会提交一个空白的“testGroup=”到服务器。


0

刚刚为其他人找到了一个适当的工作解决方案,

// 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>

在你的方法中,你可以这样使用

return $('#test2').prop('checked');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接