<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
    <div id="chkDiv">
        <input type=radio value="1" /><input type=radio value="0" /><input type=button value="TEST" onclick="test();" />
    </div>
</body>
<script>
    function test() {
        $("#chkDiv :radio").each(function () {
            console.log($(this).is(":checked"));
            console.log($(this).val());
            var trueSex = $(this).val();
            console.log(\'trueSex\', trueSex);
        })
    }
</script>
    <body>
    <div>
        <input type="checkbox" value="遍历1" />1
        <input type="checkbox" value="遍历2" />2
        <input type="checkbox" value="遍历3" />3
        <input type="checkbox" value="遍历4" />4
        <input type="checkbox" value="遍历5" />5
        <br>
        <input type="button" id="btn" value="遍历" />
    </div>
    <script>
        //  通过type属性遍历:
        $(document).ready(function () {
            $("#btn").click(function () {
                var opt = "";
                $("input[type=\'checkbox\']").each(function () {
                    if ($(this).is(":checked")) {
                        opt = "选中";
                        console.log($(this).val())
                    } else {
                        opt = "未选中";
                    }
                });
            });
        });
    </script>
</body>
</html>

 

版权声明:本文为xiufengchen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiufengchen/p/10369481.html