如何使用jquery设置单选button选定的值

如何在javascript中设置单选button的select值:

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值的脚本块将执行

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

脚本块:

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

尝试

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

也打电话给准备好的方法

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

你可以做得更优雅。

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

你也可以试试这个

 function SetRadiobuttonValue(selectedValue) { $(':radio[value="' + selectedValue + '"]').attr('checked', 'checked'); } 

下面的脚本在所有的浏

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

多谢,伙计…

如果select更改,请确保先清除其他检查。 阿拉…

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

为多个下拉菜单

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

这里RBLExperienceApplicable是单选button组input标签ID的匹配部分。 和[id^=RBLExperienceApplicable]匹配以RBLExperienceApplicable开头的所有单选button

  <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/ 演示

在这里输入图像说明

  <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; 

我find一个干净的方法是给每个单选button的ID ,然后使用下面的语句设置它:

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

这是我工作的唯一语法

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

可以使用元素的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); 
 document.getElementById("TestToggleRadioButtonList").rows[0].cells[0].childNodes[0].checked = true; 

TestToggleRadioButtonListRadioButtonList的id。