Javascript 如何使用jquery设置单选按钮的选定值

Javascript 如何使用jquery设置单选按钮的选定值,javascript,jquery,asp.net,.net,Javascript,Jquery,Asp.net,.net,如何在javascript中设置单选按钮选择值: HTML代码: <input type="radio" name="RBLExperienceApplicable" class="radio" value="1" > <input type="radio" name="RBLExperienceApplicable" class="radio" value="0" > <input type="radio" name="RBLExperienceApplic

如何在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" >
试一试

还可以在dom ready上调用该方法

<script type="text/javascript">
jQuery(function(){
    RadionButtonSelectedValueSet('RBLExperienceApplicable', '1');
})
</script>

jQuery(函数(){
RadionButtonSelectedValueSet('RBLExperienceApplied','1');
})
您也可以试试这个

function SetRadiobuttonValue(selectedValue)
{
  $(':radio[value="' + selectedValue + '"]').attr('checked', 'checked');
}
用于多个下拉列表

$('[id^=RBLExperienceApplicable][value='+ SelectedVAlue +']').attr("checked","checked");
此处
rblexperienceapplicative
是单选按钮组输入标签ID的匹配部分。 和
[id^=rblexperienceapplicative]
匹配id以
rblexperienceapplicative开头的所有单选按钮

$('input[name="RBLExperienceApplicable"]').prop('checked',true);

谢谢你,伙计…

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

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

//例如,它被选中
$(“input[type='radio']”)。更改(函数()//关于更改单选按钮
{
警报(“测试”);
if($('input[name=rblexperienceapplicative]:checked').val()
{
$('input[name=rblexperienceapplicative]:checked').val('Your value Without Quotes');
}
});


你可以做得更优雅些

function RadionButtonSelectedValueSet(name, SelectedValue) {
    $('input[name="' + name+ '"]').val([SelectedValue]);
}

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

document.getElementById('publicedit').checked = true;

可以使用元素的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>

这是唯一对我有效的语法

$('input[name="assReq"][value="' + obj["AssociationReq"] + '"]').prop('checked', 'checked');

其中
TestToggleRadioButtonList
RadioButtonList
的id

如果选择更改,请确保先清除其他检查。阿尔拉

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

您能否共享生成的html而不是ASPcode@Sam你能分享生成的html吗。。。是否有一个名称属性radios@Sam您可以从浏览器视图Source共享生成的html而不是asp代码吗谢谢..工作正常。。但是上面的脚本与一些brwser有关。。代码中的少量修改,例如:'$('input[name=“'+name+'”][value=“'+SelectdValue+'']”)attr('checked',true);'@如果使用jQuery>1.6,Sam不使用.attr()设置选中值,请参阅@Arul prop not worked in myside。。我只使用了jquery 1.3.6。。我可以继续吗?很好。此
val([…])
语法也适用于其他输入,包括
。就像我可以使用
var-inputs=$(“form:input:visible[type!=button]”)获取所有可见的输入一样
,然后使用
var-values={}获取它们的所有值;inputs.serializeArray().map(function(x){values[x.name]=x.value})
并在稍后使用
inputs.map(function(){var value=values[this.name];if(value)$(this.val([value])}
还原所有内容。调用
val([SelectedValue])
,而不是
val(SelectedValue)
,这很重要。第二种方法会将value属性设置为所有输入上的相同值。那么,这是在查找带有正确输入的选定值的单选按钮并检查它吗?对于那些想知道这是否在任何地方都有效的人,或者只是一个黑客,下面是一些文档:它们清楚地解释了使用数组会将具有匹配值的适当元素设置为selected/checked。因此,您也可以使用此选项在多选模式中选择多个复选框或选项。
  <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');

}
    });
  <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;
function RadionButtonSelectedValueSet(name, SelectedValue) {
    $('input[name="' + name+ '"]').val([SelectedValue]);
}
document.getElementById('publicedit').checked = true;
<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>
$('#' + selected).prop('checked',true);
$('input[name="assReq"][value="' + obj["AssociationReq"] + '"]').prop('checked', 'checked');
document.getElementById("TestToggleRadioButtonList").rows[0].cells[0].childNodes[0].checked = true;