当前位置:首页 > VUE

vue 实现多选框

2026-01-14 05:15:29VUE

实现多选框的基础用法

在Vue中可以使用v-model指令绑定到数组类型的数据,实现多选框功能。基础示例代码如下:

<template>
  <div>
    <input type="checkbox" id="option1" value="Option1" v-model="selectedOptions">
    <label for="option1">Option 1</label>

    <input type="checkbox" id="option2" value="Option2" v-model="selectedOptions">
    <label for="option2">Option 2</label>

    <input type="checkbox" id="option3" value="Option3" v-model="selectedOptions">
    <label for="option3">Option 3</label>

    <p>Selected options: {{ selectedOptions }}</p>
  </div>
</template>

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

动态生成多选框列表

当选项数据来自API或需要动态生成时,可以使用v-for指令:

vue 实现多选框

<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>Selected: {{ selectedOptions }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { value: 'opt1', label: 'Option 1' },
        { value: 'opt2', label: 'Option 2' },
        { value: 'opt3', label: 'Option 3' }
      ],
      selectedOptions: []
    }
  }
}
</script>

全选/全不选功能实现

添加一个控制全选的复选框,使用计算属性和方法实现全选逻辑:

vue 实现多选框

<template>
  <div>
    <input 
      type="checkbox" 
      id="selectAll" 
      v-model="allSelected"
      @change="toggleAll"
    >
    <label for="selectAll">Select All</label>

    <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>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { value: 'opt1', label: 'Option 1' },
        { value: 'opt2', label: 'Option 2' },
        { value: 'opt3', label: 'Option 3' }
      ],
      selectedOptions: []
    }
  },
  computed: {
    allSelected: {
      get() {
        return this.selectedOptions.length === this.options.length
      },
      set(value) {
        this.selectedOptions = value ? this.options.map(opt => opt.value) : []
      }
    }
  }
}
</script>

使用第三方组件库实现

Element UI的多选框组件示例:

<template>
  <el-checkbox-group v-model="checkedCities">
    <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
  </el-checkbox-group>
</template>

<script>
export default {
  data() {
    return {
      cities: ['Shanghai', 'Beijing', 'Guangzhou', 'Shenzhen'],
      checkedCities: []
    }
  }
}
</script>

表单验证中的多选框

在使用Vuelidate进行表单验证时,可以确保至少选择一个选项:

<template>
  <form @submit.prevent="submit">
    <div v-for="option in options" :key="option.value">
      <input 
        type="checkbox" 
        :value="option.value" 
        v-model="form.selectedOptions"
        @blur="$v.form.selectedOptions.$touch()"
      >
      <label>{{ option.label }}</label>
    </div>
    <div v-if="$v.form.selectedOptions.$error">
      Please select at least one option
    </div>
    <button type="submit">Submit</button>
  </form>
</template>

<script>
import { required } from 'vuelidate/lib/validators'

export default {
  data() {
    return {
      options: [
        { value: 'opt1', label: 'Option 1' },
        { value: 'opt2', label: 'Option 2' },
        { value: 'opt3', label: 'Option 3' }
      ],
      form: {
        selectedOptions: []
      }
    }
  },
  validations: {
    form: {
      selectedOptions: {
        required
      }
    }
  },
  methods: {
    submit() {
      this.$v.$touch()
      if (!this.$v.$invalid) {
        // 提交逻辑
      }
    }
  }
}
</script>

标签: 多选vue
分享给朋友:

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…