JS 表单全选取消案例

需求:

1、点击上面全选复选框,下面所有复选框都选中

2、再次点击全选复选框,下面所有的复选框都不选中

3、如果下面复选框全部选中,上面的全选按钮就会自动选中

4、如果下面复选框有一个没有选中,上面全选按钮就不选中

5、所有复选框一开始默认都没有选中

分析--难点第二部分:

需要给下面所有的复选框绑定点击事件,都要循环查看下面的复选框是否被选中,如果没有选中,上面全选就不选中

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 400px;
            height: 200px;
            margin: 20px auto;
            text-align: center;
            border-collapse: collapse;
        }
        
        thead {
            height: 50px;
            background-color: skyblue;
            font-size: 20px;
            color: white;
        }
        
        tbody {
            text-align: left;
        }
        
        td {
            border: 1px solid grey;
            padding-left: 10px;
            vertical-align: middle;
        }
        /* 鼠标经过框会有一个加强效果 */
        
        tbody tr:hover {
            background-color: pink;
        }
    </style>
</head>

<body>
    <!-- 先完成html与css部分,一个表单元素组成即可要区分thead和tbody -->
    <table>
        <thead>
            <td><input type="checkbox" class="da"></td>
            <td>商品</td>
            <td>价钱</td>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" class="xiao"></td>
                <td>iPone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="xiao"></td>
                <td>iPad Pro</td>
                <td>5000</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="xiao"></td>
                <td>iPad Air</td>
                <td>2000</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="xiao"></td>
                <td>Apple Watch</td>
                <td>2000</td>
            </tr>
        </tbody>
    </table>
    <script>
        //获得大、小复选框
        var da = document.querySelector('.da')
        var xiao = document.querySelectorAll('.xiao')
        console.log(xiao)
            //checked='checked'就是复选框被选中  (注意属性和样式的写法,不需要加style.)
            //1、选中全选框,下面所有的复选框都被选中,点击一次,表示全选,再次点击表示取消全选,使用flag来表示
            //所有复选框默认不选中
            //开始flag=0表示全选框未选中,flag=1表示选中  
        var flag = 0
        da.onclick = function() {
            //可以不定义flag,直接判断da.checked值是true还是false来判断
            if (flag == 0) {
                for (var i = 0; i < xiao.length; i++) {
                    xiao[i].checked = 'checked'
                }
                flag = 1
            } else {
                for (var i = 0; i < xiao.length; i++) {
                    xiao[i].checked = ''
                }
                flag = 0 //注意这个flag在外部循环依然是等于0的,也不知道为什么??  注册事件函数里面的值,不会改变外部的值
            }
        }

        //2、当所有的子复选框都选中的时候,则全选框被选中

        //怎么判断复选框被选中,按键按下表示复选框被选中(比较难)
        var fla = 0
        for (var j = 0; j < xiao.length; j++) {

            xiao[j].onclick = function() {
                if (this.checked) {
                    fla++
                } else {
                    fla-- //这一步缺一不可,很重要
                }
                if (fla == 4) {
                    da.checked = 'checked'
                } else {
                    da.checked = ''
                }
            }

        }
        //完成
    </script>
</body>

</html>

可以设置一个变量,来控制全选是否选中

第二部分也可以这样子更改:

        for (var i = 0; i < xiao.length; i++) { //给每个按钮绑定事件
            xiao[i].onclick = function() {
                var flag = true //控制全选按钮是否被选中

                //每次点击下面的复选框都要循环检查这四个小按钮是否全部选中
                for (var i = 0; i < xiao.length; i++) { //i不会冲突,因为作用域不同
                    if (!xiao[i].checked) { //如果有一个没有被选中,加了一个取反操作
                        flag = false;
                       break//退出for循环,提高执行效率,因为只要有一个按钮没有被选中,就不需要再判断了

                    }
                    //当整个都检查完毕之后,再让全选按钮的checked属性等于flag
                }
                da.checked = flag
            }
        }

jQuery实现商品购物全选效果:

思路:

1、里面3个小的复选框按钮选中状态跟着全选按钮走(看checked属性-表单固有属性prop获取或者设置)

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .nav {
            width: 200px;
            height: 20px;
            background-color: grey;
        }
        
        .endnav {
            width: 200px;
            height: 20px;
            background-color: pink;
        }
    </style>
    <script src="jquery.min.js"></script>
</head>

<body>
    <div class="nav"><input type="checkbox">全选</div>
    <div class="box1"><input type="checkbox" class="ckeb"></div>
    <div class="box2"><input type="checkbox" class="ckeb"></div>
    <div class="box3"><input type="checkbox" class="ckeb"></div>
    <div class="box4"><input type="checkbox" class="ckeb"></div>
    <div class="endnav"><input type="checkbox">全选</div>
    <script>
        // var inputs = document.querySelectorAll('input')
        // for (i = 1; i < inputs.length - 1; i++) {
        //     inputs[i].setAttribute('index', 'chekeetm')
        // }
        $(function() {
            //勾选第一个nav全选按钮,下面的按钮全部选中,但最后一个不选
            //首先要获取第一个按钮的状态
            //是点击一下复选框,就要获取一次,而不是直接写,这样只能获取复选框的初始状态
            // 勾选最后一个按钮,上面包括第一个全选按钮选中
            $('.nav input').change(function() {
                    var esta = $('.nav input').prop('checked')
                    var esta2 = $('.endnav input').prop('checked')
                    console.log($('.nav input').prop('checked'))
                        //当esta状态伟true的时候,下面所有的按钮状态都为选中
                        // if (esta == true) {
                        //     $('input').prop('checked', true)
                        //     $('.endnav input').prop('checked', false)
                        // } else if (esta == false) {
                        //     $('input').prop('checked', false)
                        // }
                    $('input').prop('checked', $('.nav input').prop('checked'))
                    $('.endnav input').prop('checked', false)
                })
                //给最后一个全选按钮添加勾选事件
            $('.endnav input').change(function() {
                    var esta2 = $('.endnav input').prop('checked')

                    //当esta2状态伟true的时候,上面所有的按钮状态都为选中
                    // if (esta2 == true) {
                    //     $('input').prop('checked', true)
                    // } else if (esta2 == false) {
                    //     $('input').prop('checked', false)
                    // }
                    $('input').prop('checked', $('.endnav input').prop('checked'))
                        //直接跟随状态就行
                })
                //当其中三个按钮的状态全部选中的时候,全选按钮的状态要选中,还是个其中的三个按钮设置一个类名(注意属性和类名的区别)
                //获取全选商品的状态
                // $('.ckeb').change(function() {
                //     console.log($('.ckeb').prop('checked')) //全部选中的时候,才为true,//也可以去设置一个参数,每次点击商品按钮的时候,变量变为1,如果加起来总数满足,就说明所有的按钮都被选中
                //     if ($('.ckeb').prop('checked') == true) {
                //         $('input').prop('checked', $('.ckeb').prop('checked'))
                //     } else {
                //         $('.endnav input').prop('checked', $('.ckeb').prop('checked'))
                //         $('.nav input').prop('checked', $('.ckeb').prop('checked'))

            //     }
            // })
            $('.ckeb').change(function() {
                //如果被选中的复选框个数等于4,就说明全部被选中
                // console.log($('.ckeb:checked').length)
                if ($('.ckeb:checked').length == $('.ckeb').length) {
                    $('.endnav input').prop('checked', true)
                    $('.nav input').prop('checked', true)
                } else {
                    $('.endnav input').prop('checked', false)
                    $('.nav input').prop('checked', false)
                }
            })

        })
    </script>
</body>

</html>

jQuery事先淘宝商品数量加减:

   <div class="nav"><input type="checkbox">全选</div>
    <div class="box1"> <input type="checkbox" class="ckeb"><span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"></span>
    </div>
    <div class="box2"><input type="checkbox" class="ckeb"><span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"></span></div>
    <div class="box3"><input type="checkbox" class="ckeb"> <span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"></span></div>
    <div class="box4"><input type="checkbox" class="ckeb"> <span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"></span></div>
    <div class="endnav"><input type="checkbox">全选</div>
            $('.add').click(function() {
                // alert(1)
                //要将获取出来的字符串改为数字型
                var num = parseInt($(this).siblings('.number').val()) + 1
                $(this).siblings('.number').val(num)
                    // alert($(this).siblings('.number').val())
            })
            $('.jian').click(function() {
                // alert(1)
                //要将获取出来的字符串改为数字型,只有当里面的值已经大于了2才能进行减
                if (parseInt($(this).siblings('.number').val()) >= 2) {
                    var num = parseInt($(this).siblings('.number').val()) - 1
                    $(this).siblings('.number').val(num)
                        // alert($(this).siblings('.number').val())
                }

            })

核心:首先声明一个变量,当我们点击+好,就让这个值++,然后赋值给文本框

细节:

1、只能增加本商品的数量,就是当前+号的兄弟文本框里的值

点了谁就只让他的兄弟也就是对应商品的数量发生变化,(别想着一个商品对应夜歌类名再去判断更改,这样肯定不适用)因为要集体注册时间所以每个商品里面的类名都是一样的。

2、修改表单的值是val()方法

3、这个变量的初始值应该是这个文本框的值,在这个值的基础上进行++,要获取表单的值

 4、减号对于文本框的值是1的话,那就不能再减了

 ///jQuery实现修改商品小计

 思路:

1、每次点击+或者-,根据文本框的值乘以当前商品的价格,就是商品的小计

2、只能增加本商品的小计,就是当前商品的小计模块

3、修改普通元素的内容是text()方法

4、当前商品的价格,要把¥符号去掉再相乘截取字符串substr()

要搞清除元素之间的父级、兄弟之间的对应关系,注意结构

5、number.toFixed() 确定num保留几位小数

 

            $('.add').click(function() {
                // alert(1)
                //要将获取出来的字符串改为数字型
                var num = parseInt($(this).siblings('.number').val()) + 1
                $(this).siblings('.number').val(num)
                    // alert($(this).siblings('.number').val())
                var a = $(this).siblings('.mony').text()
                    //a.substring(1)将出去¥后的数字取出来
                var b = a.substring(1)
                    //修改里面的值.只乘上初始值
                var c = num * (b / (num - 1))
                    //再将这个值赋值给mony
                $(this).siblings('.mony').text('¥' + c.toFixed(2))
                console.log(c)
            })
            $('.jian').click(function() {
                // alert(1)
                //要将获取出来的字符串改为数字型,只有当里面的值已经大于了2才能进行减
                if (parseInt($(this).siblings('.number').val()) >= 2) {
                    var num = parseInt($(this).siblings('.number').val()) - 1
                    $(this).siblings('.number').val(num)
                        // alert($(this).siblings('.number').val())
                    $(this).siblings('.mony').text('¥' + (num * ($(this).siblings('.mony').text().substring(1)) / (num + 1)).toFixed(2))
                }

            })

 

    <div class="nav"><input type="checkbox">全选</div>
    <div class="box1"> <input type="checkbox" class="ckeb"><span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"> <span class="mony">¥12.60</span></span>
    </div>
    <div class="box2"><input type="checkbox" class="ckeb"><span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"><span class="mony">¥24.80</span></span>
    </div>
    <div class="box3"><input type="checkbox" class="ckeb"> <span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"><span class="mony">¥15.90</span></span>
    </div>
    <div class="box4"><input type="checkbox" class="ckeb"> <span> <input type="button" value="←" class='jian'><input type="text" value="1" class="number"><input type="button" value="→" class="add"><span class="mony">¥20.10</span></span>
    </div>
    <div class="endnav"><input type="checkbox">全选</div>

6、用户也可以直接修改表单里面的值,同样要计算小计,用表单的change时间

7、用最新的表单内的值乘以单价即可,但还是当前商品的小计

                $('.number').change(function() {//所以此时还是需要对表单做出重新修改,单独拎出来显示商品单价})

                 还是重新开一篇文章把这个案例完整再写一遍

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一夕ξ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值