码迷,mamicode.com
首页 > Web开发 > 详细

jQuery判断checkbox,radio是否选中

时间:2016-05-07 07:47:38      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

兄弟文章: http://blog.csdn.net/libin_1/article/details/50734967

方法一:
if ($("#checkbox-id").get(0).checked) {
    // do something
}

方法二:
if($(‘#checkbox-id‘).is(‘:checked‘)) {
    // do something
}

方法三:
if ($(‘#checkbox-id‘).attr(‘checked‘)) {
    // do something
}

<!doctype html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <script type="text/javascript" src="js/jquery-2.2.2.min.js"></script>
        <title>Document</title>
        <script type="text/javascript">
            $(function() {
                $("#button").click(function() {
                    var h = $("input[type=‘checkbox‘]").length;
                    var s = ‘‘;
                    for (var i = 0; i < h; i++) {
                        if ($("input[type=‘checkbox‘]").eq(i).is(‘:checked‘)) {
                            s += $("input[type=‘checkbox‘]").get(i).value;
                        }
                    }
                    alert(s);
                })
            })
        </script>
        <script type="text/javascript">
            $(function() {
                $("#button1").click(function() {
                    var h = $("input[type=‘radio‘]").length;
                    var s = ‘‘;
                    for (var i = 0; i < h; i++) {
                        if ($("input[type=‘radio‘]").eq(i).is(‘:checked‘)) {
                            s += $("input[type=‘radio‘]").eq(i).val();
                        }
                    }
                    alert(s);
                })
            })
        </script>
    </head>

    <body>
        <input type="checkbox" name="box" id="box1" value="跳水" checked="checked" />跳水
        <input type="checkbox" name="box" id="box2" value="跑步" />跑步
        <input type="checkbox" name="box" id="box3" value="听音乐" />听音乐
        <input type="button" name="button" id="button" value="提交" />
   <br />
        <input type="radio" name="box" id="box1" value="跳水" checked="checked" />跳水
        <input type="radio" name="box" id="box2" value="跑步" />跑步
        <input type="radio" name="box" id="box3" value="听音乐" />听音乐
        <input type="button" name="button" id="button1" value="提交" />
    </body>

</html>

jQuery判断checkbox,radio是否选中

标签:

原文地址:http://blog.csdn.net/libin_1/article/details/51335322

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!