结合 Vue 的数据绑定功能来完成。以下是几种常见的实现方式: 使…">
当前位置:首页 > VUE

vue 实现单选框

2026-02-24 13:20:28VUE

Vue 实现单选框的方法

在 Vue 中实现单选框可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的数据绑定功能来完成。以下是几种常见的实现方式:

使用 v-model 绑定数据

通过 v-model 将单选框的值绑定到 Vue 实例的数据属性上,实现双向数据绑定。

vue 实现单选框

<template>
  <div>
    <label>
      <input type="radio" v-model="selectedOption" value="option1">
      Option 1
    </label>
    <label>
      <input type="radio" v-model="selectedOption" value="option2">
      Option 2
    </label>
    <p>Selected option: {{ selectedOption }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: ''
    }
  }
}
</script>

动态生成单选框

如果需要根据数据动态生成单选框,可以使用 v-for 指令。

<template>
  <div>
    <label v-for="option in options" :key="option.value">
      <input type="radio" v-model="selectedOption" :value="option.value">
      {{ option.label }}
    </label>
    <p>Selected option: {{ selectedOption }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: '',
      options: [
        { value: 'option1', label: 'Option 1' },
        { value: 'option2', label: 'Option 2' }
      ]
    }
  }
}
</script>

使用组件封装单选框

为了复用和更好的代码组织,可以封装一个单选框组件。

vue 实现单选框

<template>
  <div>
    <RadioButton 
      v-model="selectedOption" 
      value="option1" 
      label="Option 1" 
    />
    <RadioButton 
      v-model="selectedOption" 
      value="option2" 
      label="Option 2" 
    />
    <p>Selected option: {{ selectedOption }}</p>
  </div>
</template>

<script>
import RadioButton from './RadioButton.vue'

export default {
  components: {
    RadioButton
  },
  data() {
    return {
      selectedOption: ''
    }
  }
}
</script>

RadioButton.vue 组件的内容:

<template>
  <label>
    <input 
      type="radio" 
      :value="value" 
      :checked="modelValue === value"
      @change="$emit('update:modelValue', value)"
    >
    {{ label }}
  </label>
</template>

<script>
export default {
  props: {
    modelValue: {
      type: String,
      default: ''
    },
    value: {
      type: String,
      required: true
    },
    label: {
      type: String,
      required: true
    }
  }
}
</script>

处理单选框组

如果需要处理单选框组,可以将多个单选框绑定到同一个 v-model 上。

<template>
  <div>
    <h3>Choose a fruit:</h3>
    <label>
      <input type="radio" v-model="fruit" value="apple">
      Apple
    </label>
    <label>
      <input type="radio" v-model="fruit" value="banana">
      Banana
    </label>
    <label>
      <input type="radio" v-model="fruit" value="orange">
      Orange
    </label>
    <p>Selected fruit: {{ fruit }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fruit: ''
    }
  }
}
</script>

注意事项

  • 确保每个单选框的 value 属性是唯一的,避免冲突。
  • 使用 v-model 时,初始值应与单选框的 value 匹配,否则默认不会选中任何选项。
  • 如果需要默认选中某个选项,可以在 data 中设置初始值。

通过以上方法,可以灵活地在 Vue 中实现单选框功能。

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

相关文章

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-f…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascade…