javascript - 如何使用jquery设置单选按钮的选中值

标签 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="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 值的脚本 block 将执行

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

脚本 block :

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 ready上调用方法

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

关于javascript - 如何使用jquery设置单选按钮的选中值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19294001/

相关文章:

c# - Stringbuilder 文本不显示在新行上

asp.net - 处理 javascript 和 css 文件的最佳做法是什么

javascript - add_commas 函数不适用于 10000 但 1000

c# - 如何在运行时设置 recaptcha key 设置

javascript - Amchart : How the period range is selected?

javascript - 我怎样才能在中心获得按钮 "roll"?

jquery - 简单的查询表搜索过滤器

javascript - 如何从底部显示Div的部分内容?

javascript - 类似直方图的图表,具有强度彩色列和每个箱的不透明度来显示强度

javascript - 隐藏输入更改时触发 onchange 事件