当前位置:首页 > VUE

vue多选实现

2026-01-07 22:02:44VUE

Vue 多选实现方法

基础实现(v-model + multiple)

在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-model 会绑定到一个数组,用于存储选中的值。

vue多选实现

<template>
  <select v-model="selectedItems" multiple>
    <option v-for="item in items" :value="item.value">
      {{ item.text }}
    </option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      items: [
        { value: '1', text: '选项1' },
        { value: '2', text: '选项2' },
        { value: '3', text: '选项3' }
      ]
    }
  }
}
</script>

复选框组实现

如果希望使用复选框实现多选,可以通过遍历选项并为每个复选框绑定同一个数组。

vue多选实现

<template>
  <div>
    <label v-for="item in items" :key="item.value">
      <input 
        type="checkbox" 
        :value="item.value" 
        v-model="selectedItems"
      >
      {{ item.text }}
    </label>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      items: [
        { value: '1', text: '选项1' },
        { value: '2', text: '选项2' },
        { value: '3', text: '选项3' }
      ]
    }
  }
}
</script>

使用第三方组件(Element UI)

如果使用 Element UI,可以通过 el-checkbox-groupel-checkbox 快速实现多选功能。

<template>
  <el-checkbox-group v-model="selectedItems">
    <el-checkbox 
      v-for="item in items" 
      :label="item.value" 
      :key="item.value"
    >
      {{ item.text }}
    </el-checkbox>
  </el-checkbox-group>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      items: [
        { value: '1', text: '选项1' },
        { value: '2', text: '选项2' },
        { value: '3', text: '选项3' }
      ]
    }
  }
}
</script>

自定义多选组件

如果需要更灵活的控制,可以封装一个自定义多选组件。

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.value" 
      @click="toggleSelect(item.value)"
      :class="{ 'selected': selectedItems.includes(item.value) }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      items: [
        { value: '1', text: '选项1' },
        { value: '2', text: '选项2' },
        { value: '3', text: '选项3' }
      ]
    }
  },
  methods: {
    toggleSelect(value) {
      const index = this.selectedItems.indexOf(value)
      if (index === -1) {
        this.selectedItems.push(value)
      } else {
        this.selectedItems.splice(index, 1)
      }
    }
  }
}
</script>

<style>
.selected {
  background-color: #f0f0f0;
}
</style>

注意事项

  • 使用 v-model 绑定数组时,确保初始值为空数组 [],否则可能导致绑定失效。
  • 多选下拉框(<select multiple>)在移动端体验较差,建议使用复选框或第三方组件优化交互。
  • 如果选项数据动态加载,确保 v-for:key 唯一且稳定。

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

相关文章

vue实现分支

vue实现分支

Vue 实现分支的方法 在 Vue 项目中实现分支功能通常涉及条件渲染、动态组件或路由控制。以下是几种常见的实现方式: 条件渲染(v-if/v-show) 使用 Vue 的指令根据条件显示不同内容…

vue实现密码

vue实现密码

Vue 密码输入组件实现 基础密码输入框实现 使用 Vue 的 v-model 指令绑定数据,并通过 type="password" 设置输入类型为密码: <template>…

vue实现网页切换

vue实现网页切换

Vue 实现网页切换的方法 在 Vue 中实现网页切换通常可以通过以下几种方式完成,具体取决于项目需求和路由管理方式。 使用 Vue Router Vue Router 是 Vue.js 官方提供…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const rout…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…