当前位置:首页 > VUE

vue复选框实现

2026-01-20 23:35:56VUE

基础实现

在 Vue 中使用 v-model 绑定复选框的状态。通过 v-model 可以直接获取复选框的选中状态(布尔值)。

<template>
  <div>
    <input type="checkbox" v-model="isChecked" id="checkbox1">
    <label for="checkbox1">选项1</label>
    <p>当前状态: {{ isChecked }}</p>
  </div>
</template>

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

多选框组

对于多个复选框,可以将 v-model 绑定到一个数组,数组会自动收集所有选中复选框的 value 值。

vue复选框实现

<template>
  <div>
    <input type="checkbox" v-model="selectedOptions" value="option1" id="option1">
    <label for="option1">选项1</label>

    <input type="checkbox" v-model="selectedOptions" value="option2" id="option2">
    <label for="option2">选项2</label>

    <input type="checkbox" v-model="selectedOptions" value="option3" id="option3">
    <label for="option3">选项3</label>

    <p>已选选项: {{ selectedOptions }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: []
    }
  }
}
</script>

动态渲染复选框组

通过 v-for 动态渲染复选框组,适合选项数据来自后端或需要灵活配置的场景。

vue复选框实现

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

<script>
export default {
  data() {
    return {
      options: [
        { value: 'opt1', label: '动态选项1' },
        { value: 'opt2', label: '动态选项2' },
        { value: 'opt3', label: '动态选项3' }
      ],
      selectedOptions: []
    }
  }
}
</script>

自定义样式与组件

使用 <label> 包裹 <input> 实现更灵活的自定义样式,或封装为可复用组件。

<template>
  <div>
    <label class="custom-checkbox">
      <input type="checkbox" v-model="isCustomChecked">
      <span class="checkmark"></span>
      自定义样式选项
    </label>
  </div>
</template>

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

<style>
.custom-checkbox {
  display: inline-block;
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

.custom-checkbox input {
  position: absolute;
  opacity: 0;
}

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 18px;
  width: 18px;
  background-color: #eee;
  border-radius: 4px;
}

.custom-checkbox input:checked ~ .checkmark {
  background-color: #2196F3;
}
</style>

表单验证

结合 Vue 的表单验证库(如 VeeValidate)或自定义验证逻辑,实现复选框的必填验证。

<template>
  <div>
    <input 
      type="checkbox" 
      v-model="isAgreed"
      :class="{ 'error': !isAgreed && isSubmitted }"
    >
    <label>同意协议</label>
    <p v-if="!isAgreed && isSubmitted" class="error-message">必须同意协议</p>
    <button @click="submit">提交</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isAgreed: false,
      isSubmitted: false
    }
  },
  methods: {
    submit() {
      this.isSubmitted = true;
      if (!this.isAgreed) return;
      console.log('表单已提交');
    }
  }
}
</script>

<style>
.error {
  border-color: red;
}
.error-message {
  color: red;
}
</style>

标签: 复选框vue
分享给朋友:

相关文章

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Route…

vue实现tooltips

vue实现tooltips

Vue 实现 Tooltips 的方法 使用原生 HTML title 属性 在 Vue 模板中直接使用 HTML 原生 title 属性实现简单的提示效果。这种方式无需额外依赖,但功能较为基础,无法…