当前位置:首页 > VUE

vue多选实现

2026-01-07 22:02:44VUE

Vue 多选实现方法

基础实现(v-model + multiple)

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

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

复选框组实现

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

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

自定义多选组件

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

vue多选实现

<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中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…