我是靠谱客的博主 淡然可乐,这篇文章主要介绍Vue常用的全选/反选的示例代码,现在分享给大家,希望可以做个参考。

在Vue中执行CheckBox的全选反选有很多方法

我觉得最易懂,速度最快的方法就是这个!

首先就是自己创建一个input,正在mx.txt的前方添加一个input:CheckBox。在v-model加上你每次创建出来的mx.check

最重点就在于forEach遍历,出来的都是当前的。
有些人不注意的一点,为什么data里面没有check:[]这样的出现。
data里的是实时监控,你点一次自动将所有的check都变成了true。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<template> <div class="check"> <button @click="checkAll">全选</button> <br> <input type="text" v-model="txt" @keyup.enter="ck" /> <ul> <li v-for="(mx, index) in list" :key="index"> <input type="checkbox" v-model="mx.check" /> {{mx.txt}} </li> </ul> </div> </template>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<script> export default { data() { return { txt: "", list: [] } }, methods: { ck() { this.list.push({ txt: this.txt, check: false }) this.txt = "" }, checkAll() { this.list.forEach((mx) => { mx.check = !mx.check }) } } } </script>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<style lang="less"> .check { cursor: pointer; button { cursor: pointer; border: 0; padding: 10px 30px; background: #000; color: #fff; border-radius: 100px; margin-bottom: 10px; outline: none; } input { padding: 15px; width: 300px; border: 0; box-shadow: 0 0 15px #ccc; } ul { width: 300px; padding: 0; cursor: pointer; box-shadow: 0 0 15px #ccc; min-height: 300px; padding: 15px; list-style: none; li { cursor: pointer; margin-bottom: 12px; >input { padding: 0; width: auto; } } } } </style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是淡然可乐最近收集整理的关于Vue常用的全选/反选的示例代码的全部内容,更多相关Vue常用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(217)

评论列表共有 0 条评论

立即
投稿
返回
顶部