当前位置:首页 > 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管理答题状态:

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

vue实现单选多选答题

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

相关文章

vue实现多选答题

vue实现多选答题

Vue 实现多选答题功能 实现多选答题功能需要结合 Vue 的数据绑定和事件处理机制,以下是一个完整的实现方案: 数据结构设计 data() { return { questions:…

vue实现考试多选功能

vue实现考试多选功能

Vue实现考试多选功能 数据绑定与选项渲染 使用v-model绑定用户选择的答案数组,结合v-for循环渲染选项列表。示例代码: <template> <div>…

vue 实现单选

vue 实现单选

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

vue多选实现

vue多选实现

Vue多选实现方法 在Vue中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用v-model绑定数组 通过v-model绑定一个数组,当复选框被选中时,值会自动添加到数组中。…

vue如何实现单选

vue如何实现单选

使用原生 HTML 单选按钮 在 Vue 中可以直接使用 HTML 的原生单选按钮,通过 v-model 绑定数据。 <template> <div> &l…

vue实现多选答题

vue实现多选答题

实现多选答题功能 在Vue中实现多选答题功能,可以通过以下方法完成: 数据准备 定义一个questions数组,每个问题包含题目和选项,选项是一个对象数组,每个选项有文本和是否被选中的状态。…