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>

// Some Code Fetch from 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 回答
3

下面的脚本在所有浏览器中都可以正常工作:

function RadionButtonSelectedValueSet(name, SelectdValue) {
     $('input[name="' + name + '"][value="' + SelectdValue + '"]').attr('checked',true);
}
于 2013-10-10T11:57:29.297 回答
1
$('input[name="RBLExperienceApplicable"]').prop('checked',true);

多谢,伙计...

于 2013-10-10T11:21:53.493 回答
1

如果选择更改,请确保先清除其他检查。唉...

$('input[name="' + value.id + '"]').attr('checked', false);
$('input[name="' + value.id + '"][value="' + thing[value.prop] + '"]').attr('checked',  true);
               

于 2016-06-24T13:22:53.127 回答
0

对于多个下拉列表

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

RBLExperienceApplicable是单选按钮组输入标签 ID 的匹配部分。并[id^=RBLExperienceApplicable]匹配所有 id 开头的单选按钮RBLExperienceApplicable

于 2013-10-10T11:14:56.167 回答
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
  <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 回答
0

我发现一个干净的方法是ID为每个单选按钮提供一个,然后使用以下语句进行设置:

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

这是唯一对我有用的语法

$('input[name="assReq"][value="' + obj["AssociationReq"] + '"]').prop('checked', 'checked');
于 2015-02-16T20:54:28.397 回答
0
document.getElementById("TestToggleRadioButtonList").rows[0].cells[0].childNodes[0].checked = true;

TestToggleRadioButtonList的id在哪里RadioButtonList

于 2016-02-17T15:57:29.360 回答