42

javascriptでラジオボタンの選択値を設定する方法:

HTML コード :

<input type="radio" name="RBLExperienceApplicable" class="radio" value="1" > 
<input type="radio" name="RBLExperienceApplicable" class="radio" value="0" >


<input type="radio" name="RBLExperienceApplicable2" class="radio" value="1" > 
<input type="radio" name="RBLExperienceApplicable2" class="radio" value="0" >

ASPXコード

     <asp:RadioButtonList ID="RBLExperienceApplicable" runat="server" class="radio"    RepeatDirection="Horizontal" EnableViewState="false">
            <asp:ListItem Value="1" Text="Yes &nbsp;&nbsp;"></asp:ListItem> 
                <asp:ListItem Value="0" Text="No"></asp:ListItem>
        </asp:RadioButtonList>

     <asp:RadioButtonList ID="RBLExperienceApplicable2" runat="server" class="radio"  RepeatDirection="Horizontal" EnableViewState="false">
            <asp:ListItem Value="1" Text="Yes &nbsp;&nbsp;"></asp:ListItem> 
                <asp:ListItem Value="0" Text="No"></asp:ListItem>
        </asp:RadioButtonList>

// db からのコード フェッチ
// db 値に基づいて、スクリプト ブロックが実行されます

 <script type="text/javascript">
        RadionButtonSelectedValueSet('1');
 </script>

スクリプト ブロック :

function RadionButtonSelectedValueSet(SelectdValue) {
    $('#RBLExperienceApplicable').val(SelectdValue);
        //$("input[name='RBLExperienceApplicable']:checked").val(SelectdValue);
}
4

13 に答える 13

82

試す

function RadionButtonSelectedValueSet(name, SelectdValue) {
    $('input[name="' + name+ '"][value="' + SelectdValue + '"]').prop('checked', true);
}

また、dom ready でメソッドを呼び出します

<script type="text/javascript">
jQuery(function(){
    RadionButtonSelectedValueSet('RBLExperienceApplicable', '1');
})
</script>
于 2013-10-10T11:03:40.250 に答える
30

よりエレガントにできます。

function RadionButtonSelectedValueSet(name, SelectedValue) {
    $('input[name="' + name+ '"]').val([SelectedValue]);
}
于 2014-02-03T10:31:42.660 に答える
5

これも試すことができます

function SetRadiobuttonValue(selectedValue)
{
  $(':radio[value="' + selectedValue + '"]').attr('checked', 'checked');
}
于 2013-10-10T11:14:43.713 に答える
1
$('input[name="RBLExperienceApplicable"]').prop('checked',true);

ありがとう、相棒...

于 2013-10-10T11:21:53.493 に答える
0

私が見つけたクリーンなアプローチは、IDfor each ラジオボタンを指定し、次のステートメントを使用して設定することです。

document.getElementById('publicedit').checked = true;
于 2014-03-11T19:35:38.033 に答える
0

要素のIDを使用して実行できます

<label><input type="radio" name="travel_mode"  value="Flight" id="Flight"> Flight </label>
<label><input type="radio" name="travel_mode"  value="Train" id="Train"> Train </label>
<label><input type="radio" name="travel_mode"  value="Bus" id="Bus"> Bus </label>
<label><input type="radio" name="travel_mode"  value="Road" id="Road"> Other </label>

js:

$('#' + selected).prop('checked',true);
于 2014-09-22T17:12:09.273 に答える
0
document.getElementById("TestToggleRadioButtonList").rows[0].cells[0].childNodes[0].checked = true;

TestToggleRadioButtonListの ID はどこにありますかRadioButtonList

于 2016-02-17T15:57:29.360 に答える
0
  <input type="radio" name="RBLExperienceApplicable" class="radio" value="1" checked > 
 //       For Example it is checked
 <input type="radio" name="RBLExperienceApplicable" class="radio" value="0" >


<input type="radio" name="RBLExperienceApplicable2" class="radio" value="1" > 
<input type="radio" name="RBLExperienceApplicable2" class="radio" value="0" >


      $( "input[type='radio']" ).change(function() //on change radio buttons
   {


    alert('Test');
   if($('input[name=RBLExperienceApplicable]:checked').val() != '') //Testing value
 {

$('input[name=RBLExperienceApplicable]:checked').val('Your value Without Quotes');

}
    });

http://jsfiddle.net/6d6FJ/1/ デモ

ここに画像の説明を入力

于 2013-10-10T12:04:31.493 に答える
0

複数のドロップダウン用

$('[id^=RBLExperienceApplicable][value='+ SelectedVAlue +']').attr("checked","checked");

これRBLExperienceApplicableは、ラジオ ボタン グループの入力タグ ID の一致する部分です。[id^=RBLExperienceApplicable]IDがで始まるすべてのラジオボタンに一致しますRBLExperienceApplicable

于 2013-10-10T11:14:56.167 に答える
0
  <asp:RadioButtonList ID="rblRequestType">
        <asp:ListItem Selected="True" Value="value1">Value1</asp:ListItem>
        <asp:ListItem Value="Value2">Value2</asp:ListItem>
  </asp:RadioButtonList>

このようにチェックを入れることができます。

var radio0 = $("#rblRequestType_0");
var radio1 = $("#rblRequestType_1");

radio0.checked = true;
radio1.checked = true;
于 2014-01-22T14:34:10.973 に答える