js javascript 根据后台的值默认选中单选按钮,radio,单选默认值,单选扭默认

js javascript 根据后台的值默认选中单选按钮,radio,单选默认值,单选扭默认

<html>
    <head>
        <title>初始化单选</title>
        <script>
            function initradio(rName,rValue){
                var rObj = document.getElementsByName(rName);

                for(var i = 0;i < rObj.length;i++){
                    if(rObj[i].value == rValue){
                        rObj[i].checked =  'checked';
                    }
                }
            }
        </script>
    </head>
    <body>
        <form action="#">
            红色<input type="radio" name="tRadio" value="1">
            白色<input type="radio" name="tRadio" value="2">
            黑色<input type="radio" name="tRadio" value="3">
            蓝色<input type="radio" name="tRadio" value="4">
        </form>
        <script>
            initradio('tRadio',3);
        </script>
    </body>
</html>

 

你可能感兴趣的:(JavaScript)