首页 > 语言 > JavaScript > 正文

Vue常用的全选/反选的示例代码

2024-05-06 15:44:37
字体:
来源:转载
供稿:网友

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

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

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

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

<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>
<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>
<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>

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选