[英]How to set radio button selected value using jquery
How to set radio button selected value in javascript : 如何在javascript中设置单选按钮选择的值:
HTML Code : 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 Code 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>
// Some Code Fetch from db //从db获取一些代码
// Based on db value script block will execute //基于db值脚本块将执行
<script type="text/javascript">
RadionButtonSelectedValueSet('1');
</script>
Script Block : 脚本块:
function RadionButtonSelectedValueSet(SelectdValue) {
$('#RBLExperienceApplicable').val(SelectdValue);
//$("input[name='RBLExperienceApplicable']:checked").val(SelectdValue);
}
Try 尝试
function RadionButtonSelectedValueSet(name, SelectdValue) {
$('input[name="' + name+ '"][value="' + SelectdValue + '"]').prop('checked', true);
}
also call the method on dom ready 也可以在dom上调用该方法
<script type="text/javascript">
jQuery(function(){
RadionButtonSelectedValueSet('RBLExperienceApplicable', '1');
})
</script>
You can do more elegantly. 你可以做得更优雅。
function RadionButtonSelectedValueSet(name, SelectedValue) {
$('input[name="' + name+ '"]').val([SelectedValue]);
}
You can try this also 你也可以尝试一下
function SetRadiobuttonValue(selectedValue)
{
$(':radio[value="' + selectedValue + '"]').attr('checked', 'checked');
}
Below script works fine in all browser: 以下脚本适用于所有浏览器:
function RadionButtonSelectedValueSet(name, SelectdValue) {
$('input[name="' + name + '"][value="' + SelectdValue + '"]').attr('checked',true);
}
$('input[name="RBLExperienceApplicable"]').prop('checked',true);
多谢,伙计...
If the selection changes, make sure to clear the other checks first. 如果选择发生变化,请务必先清除其他检查。 al la...
我...
$('input[name="' + value.id + '"]').attr('checked', false); $('input[name="' + value.id + '"][value="' + thing[value.prop] + '"]').attr('checked', true);
for multiple dropdowns 多个下拉菜单
$('[id^=RBLExperienceApplicable][value='+ SelectedVAlue +']').attr("checked","checked");
here RBLExperienceApplicable
is the matching part of the radio button groups input tag ids. 这里
RBLExperienceApplicable
是单选按钮组输入标记ID的匹配部分。 and [id^=RBLExperienceApplicable]
matches all the radio button whose id start with RBLExperienceApplicable
和
[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');
}
});
http://jsfiddle.net/6d6FJ/1/ Demo 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>
You can set checked like this. 您可以像这样设置检查。
var radio0 = $("#rblRequestType_0");
var radio1 = $("#rblRequestType_1");
radio0.checked = true;
radio1.checked = true;
我发现一个干净的方法是为每个单选按钮提供一个ID
,然后使用以下语句进行设置:
document.getElementById('publicedit').checked = true;
Can be done using the id of the element 可以使用元素的id来完成
example 例
<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: 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.