vue小练习之全反选案例

html 复制代码
<!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>
</head>

<body>
  <div id="app">
    <div>
      <span>全选:</span>
      <input type="checkbox" v-model="isAll" />
      <button @click="reverseBtn">反选</button>
      <ul>
        <li v-for="(item,index) in arr" :key="index">
          <input type="checkbox"  v-model="item.checked"/>
          <span>{{item.name}}</span>
        </li>
      </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          {
            name: "猪八戒",
            checked: false,
          },
          {
            name: "孙悟空",
            checked: false,
          },
          {
            name: "唐僧",
            checked: false,
          },
          {
            name: "白龙马",
            checked: false,
          },
        ],
      },
      computed:{
        isAll:{
            get(){
                return this.arr.every(item=>item.checked)
            },
            set(value){
                this.arr.forEach(item=>item.checked=value)
            }

        }

      },
      methods:{
        reverseBtn(){
            this.isAll=!this.isAll
        }
      }
    })
  </script>


</body>

</html>
相关推荐
北杳同学21 小时前
前端一些用得上的有意思网站
前端·javascript·vue.js·学习
张3蜂21 小时前
CSRF Token:网络应用安全的关键防线——深度解析与实战指南
前端·安全·csrf
IT_陈寒21 小时前
Redis 性能骤降50%?这5个隐藏配置陷阱你可能从未注意过
前端·人工智能·后端
躺着听Jay21 小时前
【1267 - Illegal mix of collations 】mysql报错解决记录
java·linux·前端
Dragon Wu21 小时前
ReactNative Expo 使用总结(基础)
javascript·react native·react.js
真上帝的左手1 天前
24. 前端-js框架-Electron
前端·javascript·electron
毛发浓密的女猴子1 天前
Git Pull 策略完全指南:Merge、Rebase、Fast-forward 深度对比
前端
夏小花花1 天前
<editor> 组件设置样式不生效问题
java·前端·vue.js·xss
PieroPC1 天前
用 nicegui 3.0 + sqlite3 做个简单博客
前端·后端
weixin_307779131 天前
Jenkins Ioncions API 插件:现代化图标库在持续集成中的应用
java·运维·开发语言·前端·jenkins