当前位置:首页 > VUE

vue单选按钮实现

2026-01-07 00:17:54VUE

Vue 单选按钮实现

在 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>
    <p>Selected option: {{ selectedOption }}</p>
  </div>
</template>

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

组件化实现 将单选按钮封装为可复用的组件:

<!-- 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
  },
  emits: ['update:modelValue']
}
</script>

使用组件

vue单选按钮实现

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

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

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

样式美化 可以通过 CSS 自定义单选按钮样式:

/* 隐藏原生单选按钮 */
input[type="radio"] {
  opacity: 0;
  position: absolute;
}

/* 自定义样式 */
input[type="radio"] + label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

input[type="radio"] + label:before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 18px;
  height: 18px;
  border: 2px solid #ddd;
  border-radius: 50%;
}

input[type="radio"]:checked + label:before {
  background-color: #42b983;
  border-color: #42b983;
}

表单验证 结合 Vue 的表单验证库如 VeeValidate:

<template>
  <div>
    <input 
      type="radio" 
      id="opt1" 
      value="option1" 
      v-model="selectedOption"
      name="options"
      v-validate="'required'"
    >
    <label for="opt1">Option 1</label>
    <input 
      type="radio" 
      id="opt2" 
      value="option2" 
      v-model="selectedOption"
      name="options"
      v-validate="'required'"
    >
    <label for="opt2">Option 2</label>
    <span v-if="errors.has('options')">{{ errors.first('options') }}</span>
  </div>
</template>

以上方法涵盖了 Vue 中实现单选按钮的主要场景,包括基础用法、动态渲染、组件封装和样式定制等。

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

相关文章

css 制作按钮

css 制作按钮

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

vue实现div单选

vue实现div单选

实现 div 单选功能 在 Vue 中实现 div 单选功能可以通过多种方式完成。以下是几种常见的实现方法: 使用 v-model 和计算属性 通过 v-model 绑定一个变量,结合计算属性实现单…

js实现按钮点击

js实现按钮点击

实现按钮点击的JavaScript方法 HTML按钮元素 在HTML中创建按钮元素,为其添加id或class以便JavaScript选择: <button id="myButton">点…

vue实现按钮截图

vue实现按钮截图

使用html2canvas库实现截图 在Vue项目中安装html2canvas库 npm install html2canvas --save 创建一个可复用的截图组件 <template&…

vue隐藏按钮实现

vue隐藏按钮实现

vue隐藏按钮的实现方法 在Vue中隐藏按钮可以通过多种方式实现,以下是几种常见方法: v-if指令 根据条件动态添加或移除DOM元素,适用于需要完全移除按钮的场景: <button v-i…

vue checbox实现单选

vue checbox实现单选

使用 v-model 和计算属性 在 Vue 中,可以通过 v-model 绑定一个计算属性来实现单选效果。计算属性用于动态控制复选框的选中状态。 <template> <di…