当前位置:首页 > VUE

vue 实现多选按钮

2026-02-19 07:45:12VUE

实现多选按钮的基础方法

使用 Vue 的 v-model 绑定数组可以实现多选功能。创建一个复选框组,将选中的值存储在数组中。

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

<script>
export default {
  data() {
    return {
      options: [
        { value: 'option1', label: '选项1' },
        { value: 'option2', label: '选项2' },
        { value: 'option3', label: '选项3' }
      ],
      selectedOptions: []
    }
  }
}
</script>

使用自定义组件封装多选按钮

封装一个可复用的多选框组件,通过 props 接收选项列表,通过 emit 返回选中值。

<template>
  <div>
    <label v-for="option in options" :key="option.value">
      <input 
        type="checkbox" 
        :checked="isChecked(option.value)"
        @change="handleChange(option.value, $event)"
      >
      {{ option.label }}
    </label>
  </div>
</template>

<script>
export default {
  props: {
    options: {
      type: Array,
      required: true
    },
    value: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    isChecked(optionValue) {
      return this.value.includes(optionValue)
    },
    handleChange(optionValue, event) {
      const newValue = [...this.value]
      if (event.target.checked) {
        newValue.push(optionValue)
      } else {
        const index = newValue.indexOf(optionValue)
        if (index > -1) {
          newValue.splice(index, 1)
        }
      }
      this.$emit('input', newValue)
    }
  }
}
</script>

使用第三方库实现高级多选功能

对于更复杂的需求,可以使用如 vue-multiselect 这样的第三方库,提供搜索、分组等高级功能。

vue 实现多选按钮

安装依赖:

npm install vue-multiselect

使用示例:

vue 实现多选按钮

<template>
  <div>
    <multiselect
      v-model="selected"
      :options="options"
      :multiple="true"
      :close-on-select="false"
      placeholder="选择多个选项"
      label="label"
      track-by="value"
    ></multiselect>
  </div>
</template>

<script>
import Multiselect from 'vue-multiselect'

export default {
  components: { Multiselect },
  data() {
    return {
      selected: [],
      options: [
        { label: '选项1', value: 'option1' },
        { label: '选项2', value: 'option2' },
        { label: '选项3', value: 'option3' }
      ]
    }
  }
}
</script>

<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>

样式定制与美化

通过 CSS 自定义多选框样式,提升用户体验。使用 label 和隐藏原生 input 实现自定义样式。

<template>
  <div class="checkbox-group">
    <label 
      v-for="option in options" 
      :key="option.value"
      :class="{ 'checked': selectedOptions.includes(option.value) }"
    >
      <input 
        type="checkbox" 
        v-model="selectedOptions" 
        :value="option.value"
        class="hidden-checkbox"
      >
      <span class="custom-checkbox"></span>
      {{ option.label }}
    </label>
  </div>
</template>

<style>
.hidden-checkbox {
  position: absolute;
  opacity: 0;
}

.custom-checkbox {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 1px solid #ccc;
  margin-right: 8px;
  position: relative;
}

.checked .custom-checkbox {
  background-color: #42b983;
  border-color: #42b983;
}

.checked .custom-checkbox::after {
  content: '';
  position: absolute;
  left: 5px;
  top: 2px;
  width: 4px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
</style>

表单验证集成

结合 Vue 的表单验证库(如 VeeValidate)实现多选框的必选验证。

<template>
  <div>
    <label v-for="option in options" :key="option.value">
      <input 
        type="checkbox" 
        v-model="selectedOptions" 
        :value="option.value"
        v-validate="'required'"
        name="options"
      >
      {{ option.label }}
    </label>
    <span v-show="errors.has('options')" class="error">
      {{ errors.first('options') }}
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      options: [
        { value: 'option1', label: '选项1' },
        { value: 'option2', label: '选项2' },
        { value: 'option3', label: '选项3' }
      ]
    }
  }
}
</script>

标签: 多选按钮
分享给朋友:

相关文章

vue实现li多选

vue实现li多选

Vue 实现列表多选功能 在 Vue 中实现列表(<li>)的多选功能可以通过多种方式完成。以下是几种常见的实现方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组…

js 实现按钮点击

js 实现按钮点击

实现按钮点击的 JavaScript 方法 HTML 按钮元素 在 HTML 中创建一个按钮,可以通过 <button> 或 <input> 标签实现: <button…

vue实现按钮截图

vue实现按钮截图

使用html2canvas库实现截图 在Vue项目中安装html2canvas库 npm install html2canvas --save 创建一个可复用的截图组件 <template&…

vue实现轮播按钮

vue实现轮播按钮

实现轮播按钮的基本思路 在Vue中实现轮播按钮通常需要结合组件化思想和动态数据绑定。轮播按钮的核心功能包括自动轮播、手动切换、指示器导航等。 使用Vue实现轮播按钮 创建基础轮播组件 <tem…

vue实现按钮定位

vue实现按钮定位

Vue 实现按钮定位的方法 在 Vue 中实现按钮定位通常涉及 CSS 定位技术,结合 Vue 的响应式特性动态调整位置。以下是几种常见实现方式: 使用 CSS 固定定位 通过 position:…

vue实现多选列表

vue实现多选列表

Vue 实现多选列表的方法 使用 v-model 绑定数组 在 Vue 中,可以利用 v-model 指令绑定一个数组来实现多选功能。当用户选择多个选项时,选中的值会自动添加到数组中。 <te…