获取单选框的值

<!DOCTYPE html> 
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        window.onload = function () 
        {
            var oBtn = document.getElementById("btn");
            var oFruit = document.getElementsByName("fruit");       /*给相同的name*/
            oBtn.onclick = function () 
            {
                //使用for循环遍历所有的单选框
                for(var i=0;i<oFruit.length;i++)
                {
                    //判断当前遍历的单选框是否选中(也就是checked是否为true)
                    if(oFruit[i].checked)
                    {
                        alert(oFruit[i].value);        /*表单元素都默认有个value值*/
                    }
                }
            };
        }
    </script>
</head>
<body>
    <div>
        <label><input type="radio" name="fruit" value="苹果" checked/>苹果</label>      /*给相同的name*/
        <label><input type="radio" name="fruit" value="香蕉" />香蕉</label>
        <label><input type="radio" name="fruit" value="西瓜" />西瓜</label>
    </div>
    <input id="btn" type="button" value="获取" />
</body>
</html>
原文地址:https://www.cnblogs.com/icemiaomiao3/p/14255762.html