js或jquery怎么循环获取单选框选中的值,比如页面有8个单选框,一组2个,js如何循环获取选中值

如题所述

看下吧 不懂留言

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<input type="radio" name="a" value="a1">a1
<input type="radio" name="a" value="a2">a2
<br>
<input type="radio" name="b" value="b1">b1
<input type="radio" name="b" value="b2">b2
<br>
<input type="radio" name="c" value="c1">c1
<input type="radio" name="c" value="c2">c2
<br>
<input type="radio" name="d" value="d1">d1
<input type="radio" name="d" value="d2">d2
<br>
<button onclick="fn()">获取</button>
<script>
    var inpArr = document.getElementsByTagName("input"),
            result = ""
    function fn(){
        result = "";
        for(var i=0;i<inpArr.length;i++){
            if(inpArr[i].checked){
                result+=inpArr[i].value+" ";
            }
        }
        alert(result)
    }
</script>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考
相似回答