33

各グループの最初のラジオボタンを確認したいと思います。ただし、無効になっているラジオボタンがいくつかあるため、スクリプトはそれらを無視して、無効になっていない次のボタンに移動する必要があります。

私はこのようなものを書きましたが、うまくいきません:

$(document).ready(function(){
if($( "input ['radio'])。is(':disabled'))
    {{  
        $(this).attr('checked'、false);
    }
そうしないと
    {{
        $(this).attr('checked'、true);
    }
});

どうもありがとう

4

9 に答える 9

73

ボタンが名前属性でグループ化されている場合は、次を試してください。

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

それらが親コンテナによってグループ化されている場合、次のようになります。

$("#parentId input:radio[disabled=false]:first").attr('checked', true);
于 2010-10-20T12:34:17.633 に答える
18
$("input:radio[name=groupX]:not(:disabled):first")

これにより、グループから最初の無効化されていないラジオボタンが表示されます...

于 2010-10-20T12:28:50.487 に答える
8

以下はあなたが望むことをします:

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

デモ:JSBin

于 2010-10-20T12:27:01.737 に答える
3
<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/

于 2010-10-20T12:44:35.797 に答える
3

私は最初の答えをテストしましたが、それは解決しません。私は次の文を使わなければなりませんでした:

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

これは、name=groupNameで最初に表示されるラジオボタンをチェックします

于 2019-06-06T14:23:15.393 に答える
1

これを試して :

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

グループ内でのみ最初のラジオボタンをチェックするには、次のことができます。

 $("parentelement input:radio:not(:disabled):first-child").attr("checked", true);
于 2010-10-20T12:26:52.853 に答える
1

2020年の少しのアップグレード。公式ドキュメントでは、「first()」と「prop()」の使用が提案されています。

$("input[name='groupName'][disabled=false]").first().prop("checked", true);
于 2020-07-10T02:54:15.843 に答える
1

これにより、最初の無線がデフォルトでチェックされます

jQuery("input:radio[name=groupName]").first().click()
于 2021-02-21T11:17:41.290 に答える
1

あなたがクラスを持っていて、あなたが無効にされたアイテムについて気にしないならば、ただこれをしてください

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

または単に

$(".className").first().prop("checked", true);
于 2022-01-15T17:20:23.257 に答える