【问题标题】:Check first radio button with JQuery使用 JQuery 检查第一个单选按钮
【发布时间】:2011-04-28 00:31:13
【问题描述】:

我想检查每个组的第一个单选按钮。但是有一些单选按钮被禁用,所以脚本应该忽略它们并转到下一个非禁用按钮。

我写了这样的东西,但它不起作用:

$(document).ready(function(){
if ($("input['radio']).is(':disabled'))
    {
        $(this).attr('checked', false);
    }
别的
    {
        $(this).attr('checked', true );
    }
});

非常感谢

【问题讨论】:

  • 或者,@Haim,input:radio

标签: jquery radio-button checked


【解决方案1】:

如果您的按钮按名称属性分组,请尝试:

$("input:radio[name=groupName][disabled=false]:first").attr('checked', true);

如果它们按父容器分组,则:

$("#parentId input:radio[disabled=false]:first").attr('checked', true);

【讨论】:

  • 如果您希望更改事件触发,请改为使用.click()
  • 它有效。但是一旦设置为true,我还需要触发更改事件。
  • 我尝试了@Yarin 的“.click()”,但没有成功。但我明白了。要在设置为 true 后触发 'change' 事件, $("input:radio[name=groupName]).trigger("change"); 可以完美运行。
【解决方案2】:
$("input:radio[name=groupX]:not(:disabled):first")

这应该为您提供组中第一个未禁用的单选按钮...

【讨论】:

    【解决方案3】:

    下面做你想要的:

    $(document).ready(
      function(){
        $('input:radio:first-child').attr('checked',true);
      }
      );
    

    演示地址:JS Bin

    【讨论】:

    • 虽然,公平地说,@Šime 和 @rahul 都提供了合理的健全性检查。
    • $('input:radio:first-child') 从页面中的任何单选组中选择第一个单选按钮。
    【解决方案4】:
    <input type="radio" name="r1"><br>
    <input type="radio" name="r1"><br>
    <hr>
    <input type="radio" name="r2"><br>
    <input type="radio" name="r2"><br>
    <input type="radio" name="r2"><br>
    
    <script>
    $(function(){
        //removed duplicates form the following array
        $(jQuery.unique(
            //create an array with the names of the groups
            $('INPUT:radio')
                .map(function(i,e){
                    return $(e).attr('name') }
                ).get()
        ))
        //interate the array (array with unique names of groups)
        .each(function(i,e){
            //make the first radio-button of each group checked
            $('INPUT:radio[name="'+e+'"]:visible:first')
                .attr('checked','checked');
        });
    });
    </script>
    

    jsfiddle = http://jsfiddle.net/v4auT/

    【讨论】:

      【解决方案5】:

      我测试了第一个答案,但没有解决。我不得不使用以下句子:

      jQuery("input:radio[name=groupName]:visible")[0].checked=true;
      

      这会检查第一个可见的单选按钮 name = groupName

      【讨论】:

        【解决方案6】:

        试试这个:

         $("input:radio:not(:disabled)").attr("checked", true);
        

        要检查组中的第一个单选按钮,您可以

         $("parentelement input:radio:not(:disabled):first-child").attr("checked", true);
        

        【讨论】:

        • 我相信 OP 的意思是“组”,如“所有具有相同名称属性的单选按钮”
        【解决方案7】:

        2020年的小升级。官方文档建议使用“first()”和“prop()”

        $("input[name='groupName'][disabled=false]").first().prop("checked", true);
        

        【讨论】:

          【解决方案8】:

          这会默认检查第一个收音机

          jQuery("input:radio[name=groupName]").first().click()
          

          【讨论】:

            【解决方案9】:

            如果你有这门课并且你不关心禁用的项目,那就这样做

            $(".className").first().click();
            

            或者干脆

            $(".className").first().prop("checked", true);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-11-16
              • 1970-01-01
              • 1970-01-01
              • 2019-05-29
              • 1970-01-01
              • 2016-08-17
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多