[英]How to set radio button selected value using jquery
如何在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 "></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 "></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);
}
尝试
function RadionButtonSelectedValueSet(name, SelectdValue) {
$('input[name="' + name+ '"][value="' + SelectdValue + '"]').prop('checked', true);
}
也可以在dom上调用该方法
<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);
多谢,伙计...
如果选择发生变化,请务必先清除其他检查。 我...
$('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
是单选按钮组输入标记ID的匹配部分。 和[id^=RBLExperienceApplicable]
匹配id以RBLExperienceApplicable
开头的所有单选按钮
<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;
我发现一个干净的方法是为每个单选按钮提供一个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>
JS:
$('#' + selected).prop('checked',true);
这是唯一对我有用的语法
$('input[name="assReq"][value="' + obj["AssociationReq"] + '"]').prop('checked', 'checked');
document.getElementById("TestToggleRadioButtonList").rows[0].cells[0].childNodes[0].checked = true;
其中TestToggleRadioButtonList
是RadioButtonList
的id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.