首页 > Web开发 > 详细

jQuery 之玩转 checkbox

时间:2016-12-29 14:11:37      阅读:186      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html>

<head>
    <title></title>
    <meta charset="utf-8">
    <script type="text/javascript" src=‘http:cdn.bootcss.com/jquery/3.1.1/jquery.min.js‘></script>
</head>

<body>
    <form>
        <input type="checkbox" class="all"> 全选
        <input type="checkbox" class="reverse"> 反选
        <input type="checkbox" value="1" class="sub"> 1
        <input type="checkbox" value="2" class="sub"> 2
        <input type="checkbox" value="3" class="sub"> 3
        <input type="checkbox" value="4" class="sub"> 4
        
        

        <input type="button" value="click">
        <input type="reset">
    </form>
    <script type="text/javascript">
    $(function() {
        $(‘.all‘).on(‘click‘, function(e) {
            var isChecked = $(this).prop("checked");
            $(".sub").prop("checked", isChecked);
        })

        $(‘.reverse‘).on(‘click‘, function(e) {
            $(".sub").each(function(index, ele) {
                var checked = $(ele).is(‘:checked‘) ? ‘‘ : ‘checked‘
                $(ele).prop("checked", checked);
            })
        })

        $(‘.sub‘).on(‘click‘, function(e) {
            var checked = ($(‘.sub‘).length == $(‘.sub:checked‘).length ? ‘checked‘ : ‘‘)
            $(‘.all‘).prop(‘checked‘, checked)
        })

        $("input[type=‘button‘]").bind(‘click‘,function(){
            var obj = $(‘.sub‘).map(function(index,ele){
                return $(ele).val()
            })
            var arr = Array.prototype.slice.call(obj)
            alert(JSON.stringify(arr))
        })
    })
    </script>
</body>

</html>

 

jQuery 之玩转 checkbox

原文:http://www.cnblogs.com/xuezizhenchengxuyuan/p/6232472.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!