当前位置:首页 > VUE

vue实现单选多选答题

2026-01-23 00:08:30VUE

Vue实现单选答题

创建单选答题组件需要绑定v-model到同一个变量,确保同一时间只能选中一个选项。以下是一个基础实现示例:

<template>
  <div>
    <h3>单选题</h3>
    <div v-for="(option, index) in options" :key="index">
      <input 
        type="radio" 
        :id="'option' + index" 
        :value="option.value" 
        v-model="selectedAnswer"
      >
      <label :for="'option' + index">{{ option.text }}</label>
    </div>
    <p>当前选择: {{ selectedAnswer }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedAnswer: null,
      options: [
        { text: "选项A", value: "A" },
        { text: "选项B", value: "B" },
        { text: "选项C", value: "C" }
      ]
    }
  }
}
</script>

Vue实现多选答题

多选答题需要绑定到数组类型变量,并使用v-model配合checkbox类型输入框:

<template>
  <div>
    <h3>多选题</h3>
    <div v-for="(option, index) in options" :key="index">
      <input 
        type="checkbox" 
        :id="'option' + index" 
        :value="option.value" 
        v-model="selectedAnswers"
      >
      <label :for="'option' + index">{{ option.text }}</label>
    </div>
    <p>当前选择: {{ selectedAnswers }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedAnswers: [],
      options: [
        { text: "选项A", value: "A" },
        { text: "选项B", value: "B" },
        { text: "选项C", value: "C" }
      ]
    }
  }
}
</script>

动态题目加载

从API获取题目数据时,可以这样处理:

export default {
  data() {
    return {
      questions: [],
      currentQuestionIndex: 0,
      answers: {}
    }
  },
  async created() {
    const response = await fetch('/api/questions')
    this.questions = await response.json()
  },
  methods: {
    goToNextQuestion() {
      if (this.currentQuestionIndex < this.questions.length - 1) {
        this.currentQuestionIndex++
      }
    }
  }
}

表单验证与提交

添加验证逻辑和提交功能:

<template>
  <form @submit.prevent="submitAnswers">
    <!-- 单选或多选题组件 -->
    <button type="submit" :disabled="!isFormValid">提交答案</button>
  </form>
</template>

<script>
export default {
  computed: {
    isFormValid() {
      return this.selectedAnswer !== null // 单选验证
      // 或多选验证 return this.selectedAnswers.length > 0
    }
  },
  methods: {
    submitAnswers() {
      // 提交逻辑
    }
  }
}
</script>

使用第三方UI库

使用Element UI实现更美观的界面:

<template>
  <el-radio-group v-model="selectedAnswer">
    <el-radio 
      v-for="(option, index) in options" 
      :key="index" 
      :label="option.value"
    >
      {{ option.text }}
    </el-radio>
  </el-radio-group>

  <el-checkbox-group v-model="selectedAnswers">
    <el-checkbox 
      v-for="(option, index) in options" 
      :key="index" 
      :label="option.value"
    >
      {{ option.text }}
    </el-checkbox>
  </el-checkbox-group>
</template>

状态管理

对于复杂应用,建议使用Vuex管理答题状态:

vue实现单选多选答题

// store.js
export default new Vuex.Store({
  state: {
    answers: {}
  },
  mutations: {
    setAnswer(state, { questionId, answer }) {
      state.answers[questionId] = answer
    }
  }
})

标签: 多选单选
分享给朋友:

相关文章

vue实现div单选

vue实现div单选

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

vue实现单选按钮

vue实现单选按钮

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

vue 如何实现多选

vue 如何实现多选

Vue 实现多选的几种方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选功能。当用户勾选多个选项时,选中的值会自动添加到数组中。 <temp…

vue实现多选效果

vue实现多选效果

Vue 实现多选效果的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选效果。适用于复选框组(checkbox)或下拉多选(select multi…

vue实现多选日历

vue实现多选日历

Vue 实现多选日历 使用 Vue 实现多选日历功能可以借助现有的日历库或手动构建。以下是两种常见方法: 使用第三方库(如 V-Calendar) 安装 V-Calendar 库: npm ins…

vue 实现日期多选

vue 实现日期多选

实现日期多选的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选。日期选择器组件(如 el-date-picker)需设置 type="dates"…