js获取复选框内容

<!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>

 

posted @ 2019-02-13 13:59  菩提树下的丁春秋  阅读(1929)  评论(0编辑  收藏  举报