当前位置:首页 > VUE

vue实现checkbox选取

2026-01-16 06:10:46VUE

Vue 实现 Checkbox 选取

在 Vue 中实现 Checkbox 的选取功能可以通过 v-model 双向绑定数据,结合数组或布尔值来管理选中状态。以下是几种常见实现方式:

单个 Checkbox 绑定布尔值

适用于单个复选框的场景,绑定一个布尔值表示选中状态。

<template>
  <div>
    <input type="checkbox" id="checkbox" v-model="isChecked" />
    <label for="checkbox">选择状态:{{ isChecked }}</label>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isChecked: false
    };
  }
};
</script>

多个 Checkbox 绑定数组

适用于多个复选框的场景,绑定一个数组来存储选中的值。

<template>
  <div>
    <input type="checkbox" id="option1" value="选项1" v-model="selectedOptions" />
    <label for="option1">选项1</label>
    <input type="checkbox" id="option2" value="选项2" v-model="selectedOptions" />
    <label for="option2">选项2</label>
    <input type="checkbox" id="option3" value="选项3" v-model="selectedOptions" />
    <label for="option3">选项3</label>
    <p>已选:{{ selectedOptions }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: []
    };
  }
};
</script>

动态生成 Checkbox 列表

适用于动态渲染复选框的场景,通常结合 v-for 循环生成选项。

<template>
  <div>
    <div v-for="option in options" :key="option.value">
      <input 
        type="checkbox" 
        :id="option.value" 
        :value="option.value" 
        v-model="selectedOptions" 
      />
      <label :for="option.value">{{ option.label }}</label>
    </div>
    <p>已选:{{ selectedOptions }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { value: 'opt1', label: '选项A' },
        { value: 'opt2', label: '选项B' },
        { value: 'opt3', label: '选项C' }
      ],
      selectedOptions: []
    };
  }
};
</script>

全选与反选功能

通过计算属性或方法实现全选/反选逻辑。

vue实现checkbox选取

<template>
  <div>
    <input type="checkbox" id="selectAll" v-model="allSelected" @change="toggleAll" />
    <label for="selectAll">全选</label>
    <div v-for="item in items" :key="item.id">
      <input type="checkbox" :id="item.id" :value="item.id" v-model="selectedItems" />
      <label :for="item.id">{{ item.name }}</label>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' },
        { id: 3, name: '项目3' }
      ],
      selectedItems: []
    };
  },
  computed: {
    allSelected: {
      get() {
        return this.selectedItems.length === this.items.length;
      },
      set(value) {
        this.selectedItems = value ? this.items.map(item => item.id) : [];
      }
    }
  },
  methods: {
    toggleAll() {
      this.allSelected = !this.allSelected;
    }
  }
};
</script>

注意事项

  • 使用 v-model 时,确保绑定的数据类型匹配(数组或布尔值)。
  • 动态生成的复选框需要为 value 属性赋值,否则无法正确收集选中值。
  • 全选功能可通过计算属性的 getset 方法简化逻辑。

标签: vuecheckbox
分享给朋友:

相关文章

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

vue聊天实现

vue聊天实现

Vue 聊天应用实现步骤 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm c…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue底部实现

vue底部实现

Vue 底部实现方法 在 Vue 项目中实现底部布局可以通过多种方式完成,以下是一些常见的方法: 使用固定定位 将底部元素固定在页面底部,适用于单页应用或需要始终显示的底部栏。 <temp…