【问题标题】:Check whether specific radio button is checked检查是否选中了特定的单选按钮
【发布时间】:2011-01-12 19:12:25
【问题描述】:

查看 jQuery 文档后我遇到了麻烦。我只是想在我的 jquery 方法中返回真/假,具体取决于对某个单选按钮的检查以及它是否被选中

我已经尝试了几件事,但未能做到这一点:

<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"); 上收到未定义的消息

更新:

示例:

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

这仍然返回“未定义”:

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

如果我尝试这个,即使我选择了单选按钮,我也会得到“错误”:

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

【问题讨论】:

    标签: jquery radio-button


    【解决方案1】:
    $("input[@name='<%=test2.ClientID%>']:checked");
    

    使用这个和这里的 ClientID 获取由 .net 创建的随机 id。

    【讨论】:

    • 问题是在 6 年前提出的。他/她也得到了答案。你不能说use this
    【解决方案2】:

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

    // 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');
    

    【讨论】:

      【解决方案3】:

      您的选择器不会选择输入字段,如果选择了,它将返回一个 jQuery 对象。试试这个:

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

      【讨论】:

      • 即使我选择让我们说 test2,它每次都返回 false
      • Dear God .NET 忘记了它为 ID 创建的唯一随机 clientID。
      【解决方案4】:

      为什么要为所有花哨的选择器烦恼?如果您正确使用了这些 id="" 属性,那么 'test2' 必须是页面上唯一具有该 id 的标签,然后 .checked 布尔属性会告诉您它是否被选中:

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

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

      【讨论】:

        【解决方案5】:

        我认为您使用了错误的方法。您应该设置输入元素的value 属性。查看the docs for .val() 获取设置和返回输入元素的 .val() 的示例。

        即。

        <input type="radio" runat="server" name="testGroup" value="test2" />
        
        return $('input:radio[name=testGroup]:checked').val() == 'test2';
        

        【讨论】:

        • 好吧,我现在不能去改变这一切。这需要对我们当前的页面进行大量重构。
        • 正则表达式的胜利!只需将您的 id 替换为值即可。重点是,输入元素(尤其是复选框和单选按钮)应该有一个值。更多信息:w3.org/TR/html401/interact/forms.html#h-17.4
        • $('input:radio[name=bar]:checked').val();所以我们在那里没有价值我想分别默认为 0,1 和 2
        • 可能是这样,但我不确定您是否可以依赖它。请参阅我链接的 w3.org 文档——value 是可选的,除非 type 属性的值为“radio”或“checkbox”。
        • 当我选择按钮时,根据您的示例,我仍然得到错误
        【解决方案6】:

        1.属性名不再需要@前缀:

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

        注意:在 jQuery 1.3 [@attr] 样式中 选择器被删除(它们是 以前在 jQuery 1.2 中已弃用)。 只需从您的 选择器以使它们工作 再次。

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

        【讨论】:

        • 井名对于所有 3 个单选按钮来说都是相同的,因为在这种情况下它是一个组。
        • 没错,它是一样的,但你仍然需要它
        • 我有名字...所以不确定是什么问题?? :(
        • 问题是name='test2' -- 你的电台组的名字实际上是testGroup
        【解决方案7】:

        您应该删除“名称”之前的“@”;它不再需要(对于当前的 jQuery 版本)。

        您想要返回名称为“test2”的所有已检查元素,但您没有任何具有该名称的元素,您使用的是“test2”的id

        如果您要使用 ID,请尝试:

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

        【讨论】:

        • 我仍然得到一个未定义的..它不知道#test2 是什么。
        猜你喜欢
        • 2018-12-12
        • 2016-09-26
        • 2012-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        相关资源
        最近更新 更多