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>


黑色头发:http://heisetoufa.iteye.com

你可能感兴趣的:(html,js,script)