vue全选和取消全选(无bug)

很简单的使用vue实现全选和取消全选

直接上代码,简单易懂不懂得可以留言。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
    <div id="app">
            <div v-for="list,index in lists">
                <label :for="index">
                    {{list.name}}<input :id="index" type="checkbox" :value="list.name" v-model="checkboxArr"> //这里注意list.name这个值一定要和checkAll函数中绑定的红色的值要一样
                </label>
            </div>
        <label>
            <input type="checkbox" class="checkbox" @click="selectAll" />{{checkAllAddTxt}}
        </label>
    </div>
    <script src="vue.js"></script>
    <script>
        window.onload = function () {
            var c = new Vue({
                el: '#app',
                data: {
                    checkboxArr: [],
                    lists: [{ // lists是从接口拿到的要遍历带有checkbox的数据
                        'name': '111'
                    }, {
                        'name': '222'
                    }, {
                        'name': '333'
                    }, {
                        'name': '444'
                    }],
                    checkAllAddTxt: '全选' // 作为全选 取消全选的显示文字
                },
                methods: {
                    selectAll: function (event) {
                        var _this = this;
                        if (!event.currentTarget.checked) {
                            this.checkboxArr = []; // 取消全选
                            this.checkAllAddTxt = '全选';
                        } else { // 实现全选
                            _this.checkboxArr = [];
                            _this.lists.forEach(function (list, i) {
                                _this.checkboxArr.push(list.name);
                            });
                            this.checkAllAddTxt = '取消全选';
                        }
                    }
                }
            });
        };
    </script>
</body>

</html>

 

转载于:https://www.cnblogs.com/lm-it/p/8467400.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值