如何使用JavaScript设置radio选中的示例

黄舟
黄舟 原创
2017-07-27 14:21:23 2679浏览

在页面数据绑定时,经常会遇到给radio设置选中,以下是我写的js方法,经测试可以使用。欢迎拍砖


<html>
<head>
    <script type="text/javascript" src="./jquery.min.js"></script></head><body><p>
    <input id="rdo1" name="rdo1" type="radio" value="1" checked="checked"/>是 
    <input id="rdo1" name="rdo1" type="radio" value="0"/>否    
    <button id="btn1">是</button>
    <button id="btn2">否</button><p><script type="text/javascript">
    $(function(){
        $("#btn1").click(function(){
            $("input[name='rdo1']").eq(0).attr("checked","checked");
            $("input[name='rdo1']").eq(1).removeAttr("checked");
            $("input[name='rdo1']").eq(0).click();
        });
        $("#btn2").click(function(){
            $("input[name='rdo1']").eq(0).removeAttr("checked");
            $("input[name='rdo1']").eq(1).attr("checked","checked");
            $("input[name='rdo1']").eq(1).click();
        });
    });</script></body></html>

以上就是如何使用JavaScript设置radio选中的示例的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。