jQuery 如何使用jquery设置单选按钮选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19294001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set radio button selected value using jquery
提问by Arun P Johny
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
// Based on db value script block will execute
// Some Code Fetch from db
// 基于db值的脚本块将执行
<script type="text/javascript">
RadionButtonSelectedValueSet('1');
</script>
Script Block :
脚本块:
function RadionButtonSelectedValueSet(SelectdValue) {
$('#RBLExperienceApplicable').val(SelectdValue);
//$("input[name='RBLExperienceApplicable']:checked").val(SelectdValue);
}
回答by Arun P Johny
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>
回答by Shuhei Kagawa
You can do more elegantly.
你可以做得更优雅。
function RadionButtonSelectedValueSet(name, SelectedValue) {
$('input[name="' + name+ '"]').val([SelectedValue]);
}
回答by Avinash Jha
You can try this also
你也可以试试这个
function SetRadiobuttonValue(selectedValue)
{
$(':radio[value="' + selectedValue + '"]').attr('checked', 'checked');
}
回答by Avinash Jha
Below script works fine in all browser:
下面的脚本在所有浏览器中都可以正常工作:
function RadionButtonSelectedValueSet(name, SelectdValue) {
$('input[name="' + name + '"][value="' + SelectdValue + '"]').attr('checked',true);
}
回答by MrSethT
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);
回答by Nandakumar
$('input[name="RBLExperienceApplicable"]').prop('checked',true);
Thanks dude...
多谢,伙计...
回答by th1rdey3
for multiple dropdowns
对于多个下拉菜单
$('[id^=RBLExperienceApplicable][value='+ SelectedVAlue +']').attr("checked","checked");
here RBLExperienceApplicable
is the matching part of the radio button groups input tag ids.
and [id^=RBLExperienceApplicable]
matches all the radio button whose id start with RBLExperienceApplicable
这RBLExperienceApplicable
是单选按钮组输入标签 ID 的匹配部分。并[id^=RBLExperienceApplicable]
匹配所有 id 开头的单选按钮RBLExperienceApplicable
回答by Fahim
A clean approach I find is by giving an ID
for each radio button and then setting it by using the following statement:
我发现一个干净的方法是ID
为每个单选按钮提供一个,然后使用以下语句设置它:
document.getElementById('publicedit').checked = true;
回答by chandramohan
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);
回答by pat capozzi
This is the only syntax that worked for me
这是唯一对我有用的语法
$('input[name="assReq"][value="' + obj["AssociationReq"] + '"]').prop('checked', 'checked');