当前位置:首页 > VUE

vue实现多选框

2026-02-20 00:11:52VUE

Vue 实现多选框的方法

在 Vue 中实现多选框可以通过多种方式,以下是几种常见的实现方法。

使用 v-model 绑定数组

Vue 的 v-model 可以绑定到一个数组,用于收集多选框的选中值。每个多选框的 value 属性会被添加到数组中。

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

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

使用 true-valuefalse-value

如果需要自定义多选框选中和未选中时的值,可以使用 true-valuefalse-value

<template>
  <div>
    <input
      type="checkbox"
      v-model="checked"
      :true-value="'选中'"
      :false-value="'未选中'"
    />
    <p>状态: {{ checked }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      checked: '未选中',
    };
  },
};
</script>

动态生成多选框组

结合 v-for 动态生成多选框组,适用于选项从后端获取的场景。

<template>
  <div>
    <label v-for="item in items" :key="item.id">
      <input
        type="checkbox"
        v-model="selectedItems"
        :value="item.id"
      />
      {{ item.name }}
    </label>
    <p>选中的项目: {{ selectedItems }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' },
        { id: 3, name: '项目3' },
      ],
      selectedItems: [],
    };
  },
};
</script>

使用计算属性处理选中状态

通过计算属性对多选框的选中状态进行逻辑处理。

<template>
  <div>
    <label v-for="fruit in fruits" :key="fruit.id">
      <input
        type="checkbox"
        v-model="selectedFruits"
        :value="fruit.id"
      />
      {{ fruit.name }}
    </label>
    <p>选中的水果: {{ selectedFruitNames }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fruits: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' },
      ],
      selectedFruits: [],
    };
  },
  computed: {
    selectedFruitNames() {
      return this.fruits
        .filter(fruit => this.selectedFruits.includes(fruit.id))
        .map(fruit => fruit.name);
    },
  },
};
</script>

使用第三方组件库

如果需要更丰富的功能,可以使用第三方组件库如 Element UI、Vuetify 或 Ant Design Vue。

以 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: ['上海', '北京', '广州', '深圳'],
      checkedCities: [],
    };
  },
};
</script>

以上方法覆盖了从基础到进阶的多选框实现方式,可以根据实际需求选择合适的方法。

vue实现多选框

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

相关文章

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue实现旋转

vue实现旋转

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

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…