当前位置:首页 > VUE

vue实现checkbox选取

2026-03-29 06:41:20VUE

实现基础 Checkbox 绑定

在 Vue 中可以通过 v-model 实现双向数据绑定。对于单个 checkbox,绑定的是布尔值:

<template>
  <input type="checkbox" v-model="isChecked" />
  <label>{{ isChecked ? '已选中' : '未选中' }}</label>
</template>

<script>
export default {
  data() {
    return {
      isChecked: false
    }
  }
}
</script>

处理多个 Checkbox 选项

当需要处理多个选项时,可以将 v-model 绑定到数组:

vue实现checkbox选取

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

<script>
export default {
  data() {
    return {
      options: [
        { value: 'apple', label: '苹果' },
        { value: 'banana', label: '香蕉' },
        { value: 'orange', label: '橙子' }
      ],
      selectedOptions: []
    }
  }
}
</script>

自定义 Checkbox 组件

创建可复用的 Checkbox 组件:

<!-- Checkbox.vue -->
<template>
  <label class="checkbox-container">
    <input 
      type="checkbox" 
      :checked="modelValue" 
      @change="$emit('update:modelValue', $event.target.checked)"
    />
    <span class="checkmark"></span>
    <slot></slot>
  </label>
</template>

<script>
export default {
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:modelValue']
}
</script>

<style>
.checkbox-container {
  display: inline-block;
  position: relative;
  cursor: pointer;
}
/* 自定义样式代码... */
</style>

使用 v-model 与计算属性

对于需要处理的复杂逻辑,可以使用计算属性:

vue实现checkbox选取

<template>
  <input type="checkbox" v-model="computedChecked" />
</template>

<script>
export default {
  props: ['checked'],
  emits: ['update:checked'],
  computed: {
    computedChecked: {
      get() {
        return this.checked
      },
      set(value) {
        this.$emit('update:checked', value)
      }
    }
  }
}
</script>

动态渲染 Checkbox 列表

结合 API 数据动态渲染选项:

<template>
  <div v-if="loading">加载中...</div>
  <div v-else>
    <div v-for="item in apiData" :key="item.id">
      <input 
        type="checkbox" 
        :value="item.id" 
        v-model="selectedItems"
      />
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      apiData: [],
      selectedItems: [],
      loading: true
    }
  },
  async created() {
    try {
      const response = await fetch('api/items')
      this.apiData = await response.json()
    } finally {
      this.loading = false
    }
  }
}
</script>

全选/反选功能实现

添加全选功能需要额外处理:

<template>
  <input 
    type="checkbox" 
    v-model="selectAll" 
    @change="toggleAll"
  /> 全选
  <div v-for="item in items" :key="item.id">
    <input 
      type="checkbox" 
      :value="item.id" 
      v-model="selectedIds"
    />
    {{ item.name }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' },
        { id: 3, name: '选项3' }
      ],
      selectedIds: []
    }
  },
  computed: {
    selectAll: {
      get() {
        return this.selectedIds.length === this.items.length
      },
      set(value) {
        this.selectedIds = value ? this.items.map(item => item.id) : []
      }
    }
  },
  methods: {
    toggleAll() {
      this.selectAll = !this.selectAll
    }
  }
}
</script>

标签: vuecheckbox
分享给朋友:

相关文章

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…