<…">
当前位置:首页 > VUE

vue实现单选按钮

2026-01-14 08:06:06VUE

实现单选按钮的基本方法

在Vue中实现单选按钮通常使用v-model指令绑定数据,结合input元素的type="radio"属性。以下是一个基础示例:

<template>
  <div>
    <input type="radio" id="option1" value="Option 1" v-model="selectedOption">
    <label for="option1">Option 1</label>

    <input type="radio" id="option2" value="Option 2" v-model="selectedOption">
    <label for="option2">Option 2</label>

    <p>Selected option: {{ selectedOption }}</p>
  </div>
</template>

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

动态生成单选按钮组

当选项需要从数据动态生成时,可以使用v-for指令:

vue实现单选按钮

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

<script>
export default {
  data() {
    return {
      selectedOption: '',
      options: [
        { value: 'A', label: 'Option A' },
        { value: 'B', label: 'Option B' },
        { value: 'C', label: 'Option C' }
      ]
    }
  }
}
</script>

使用组件封装单选按钮

创建可复用的单选按钮组件:

vue实现单选按钮

<!-- RadioButton.vue -->
<template>
  <div>
    <input 
      type="radio" 
      :id="id" 
      :value="value" 
      v-model="modelValue"
      @change="$emit('update:modelValue', value)"
    >
    <label :for="id">{{ label }}</label>
  </div>
</template>

<script>
export default {
  props: {
    id: String,
    value: String,
    label: String,
    modelValue: String
  }
}
</script>

表单验证集成

结合Vuelidate进行表单验证:

<template>
  <form @submit.prevent="submitForm">
    <div v-for="option in options" :key="option.value">
      <input
        type="radio"
        :id="option.value"
        :value="option.value"
        v-model="form.selectedOption"
        @blur="$v.form.selectedOption.$touch()"
      >
      <label :for="option.value">{{ option.label }}</label>
    </div>
    <span v-if="$v.form.selectedOption.$error">Please select an option</span>

    <button type="submit">Submit</button>
  </form>
</template>

<script>
import { required } from 'vuelidate/lib/validators'

export default {
  data() {
    return {
      form: {
        selectedOption: null
      },
      options: [
        { value: 'yes', label: 'Yes' },
        { value: 'no', label: 'No' }
      ]
    }
  },
  validations: {
    form: {
      selectedOption: { required }
    }
  },
  methods: {
    submitForm() {
      this.$v.$touch()
      if (!this.$v.$invalid) {
        // 提交逻辑
      }
    }
  }
}
</script>

样式定制方法

使用CSS自定义单选按钮样式:

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

<script>
export default {
  data() {
    return {
      selectedOption: '',
      options: [
        { value: '1', label: 'Red' },
        { value: '2', label: 'Blue' },
        { value: '3', label: 'Green' }
      ]
    }
  }
}
</script>

<style>
.radio-input {
  position: absolute;
  opacity: 0;
}

.radio-custom {
  position: relative;
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-right: 8px;
  border: 2px solid #ccc;
  border-radius: 50%;
  vertical-align: middle;
}

.radio-input:checked + .radio-custom {
  background-color: #4CAF50;
  border-color: #4CAF50;
}

.radio-input:checked + .radio-custom::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 8px;
  height: 8px;
  background: white;
  border-radius: 50%;
}

.radio-label {
  display: block;
  margin-bottom: 8px;
  cursor: pointer;
}
</style>

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

相关文章

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到…

css 制作按钮

css 制作按钮

基础按钮样式 使用CSS创建一个基础按钮需要定义padding、background-color、border和border-radius等属性。以下是一个简单示例: .button { pad…

css按钮制作

css按钮制作

基础按钮样式 使用CSS创建基础按钮需要定义padding、background-color、border和border-radius等属性。以下是一个简单的按钮样式示例: .button {…

css按钮在线制作

css按钮在线制作

CSS按钮在线制作工具 以下是一些可用于在线制作CSS按钮的工具和资源,帮助快速生成美观且响应式的按钮样式。 Button Generator Button Generator提供可视化编辑界面,…

css制作炫酷按钮

css制作炫酷按钮

渐变背景按钮 使用CSS的linear-gradient属性创建渐变背景效果,结合阴影和过渡动画增强视觉冲击力。按钮悬停时通过改变渐变角度或颜色实现动态效果。 .gradient-btn { b…

vue单选按钮实现

vue单选按钮实现

Vue 单选按钮实现方法 在 Vue 中实现单选按钮可以通过 v-model 指令绑定数据,结合 <input type="radio"> 或第三方组件库完成。以下是几种常见实现方式:…