当前位置:首页 > 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 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue 实现协同

vue 实现协同

Vue 实现协同编辑的方案 协同编辑指多个用户同时编辑同一文档并实时同步更改。Vue 结合相关库和技术可轻松实现该功能。 使用 WebSocket 实现实时通信 WebSocket 提供全双工通信,…

vue实现数据渲染

vue实现数据渲染

Vue 数据渲染的实现方式 Vue.js 提供了多种数据渲染的方式,包括插值表达式、指令和计算属性等。以下是常见的实现方法: 插值表达式 使用双大括号 {{ }} 将数据绑定到模板中,数据变化时会自…

vue实现轨迹定位

vue实现轨迹定位

Vue实现轨迹定位的方法 在Vue中实现轨迹定位功能通常需要结合地图API(如高德、百度或Google Maps)和定位API(如HTML5 Geolocation或第三方SDK)。以下是几种常见的方…

vue修改功能实现

vue修改功能实现

Vue 修改功能的实现方法 在 Vue 中实现修改功能通常涉及表单绑定、数据更新和事件处理。以下是几种常见的实现方式: 数据双向绑定 使用 v-model 指令实现表单元素与 Vue 实例数据的双向…