当前位置:首页 > VUE

vue实现单选答题功能

2026-02-23 21:48:57VUE

实现单选答题功能的基本思路

在Vue中实现单选答题功能,可以通过v-model绑定单选按钮组的值,结合计算属性和方法来实现答题逻辑。以下是一个完整的实现方案。

创建单选问题数据

在Vue组件的data中定义问题和选项:

data() {
  return {
    questions: [
      {
        id: 1,
        text: 'Vue.js的作者是谁?',
        options: [
          { id: 'a', text: '尤雨溪' },
          { id: 'b', text: 'Dan Abramov' },
          { id: 'c', text: 'Evan You' }
        ],
        correctAnswer: 'a',
        selectedAnswer: null
      },
      // 可以添加更多问题
    ],
    currentQuestionIndex: 0
  }
}

模板中的单选按钮组

使用v-for渲染选项,v-model绑定选中值:

<div v-for="(question, index) in questions" :key="question.id" v-show="index === currentQuestionIndex">
  <h3>{{ question.text }}</h3>
  <div v-for="option in question.options" :key="option.id">
    <input 
      type="radio" 
      :id="`q${question.id}_${option.id}`" 
      :value="option.id" 
      v-model="question.selectedAnswer"
    />
    <label :for="`q${question.id}_${option.id}`">{{ option.text }}</label>
  </div>
</div>

添加导航和提交功能

<button @click="prevQuestion" :disabled="currentQuestionIndex === 0">上一题</button>
<button @click="nextQuestion" :disabled="currentQuestionIndex === questions.length - 1">下一题</button>
<button @click="submitAnswers">提交答案</button>

实现导航方法

methods: {
  prevQuestion() {
    if (this.currentQuestionIndex > 0) {
      this.currentQuestionIndex--
    }
  },
  nextQuestion() {
    if (this.currentQuestionIndex < this.questions.length - 1) {
      this.currentQuestionIndex++
    }
  },
  submitAnswers() {
    const score = this.questions.filter(
      q => q.selectedAnswer === q.correctAnswer
    ).length
    alert(`您的得分是: ${score}/${this.questions.length}`)
  }
}

显示答题进度和结果

添加计算属性显示进度:

computed: {
  progress() {
    return ((this.currentQuestionIndex + 1) / this.questions.length * 100).toFixed(0) + '%'
  },
  answeredCount() {
    return this.questions.filter(q => q.selectedAnswer !== null).length
  }
}

在模板中添加进度显示:

<div>
  进度: {{ progress }} ({{ answeredCount }}/{{ questions.length }})
</div>

添加样式反馈

可以为正确/错误答案添加样式反馈:

.correct {
  background-color: #d4edda;
}
.incorrect {
  background-color: #f8d7da;
}

在提交后显示结果:

vue实现单选答题功能

submitAnswers() {
  this.questions.forEach(q => {
    q.isCorrect = q.selectedAnswer === q.correctAnswer
  })
  this.showResults = true
}

完整组件示例

export default {
  data() {
    return {
      questions: [...], // 问题数据
      currentQuestionIndex: 0,
      showResults: false
    }
  },
  computed: {
    // 计算属性
  },
  methods: {
    // 方法实现
  }
}

通过以上步骤,可以在Vue中实现一个完整的单选答题功能,包括问题展示、答案选择、导航控制和结果反馈。

标签: 单选功能
分享给朋友:

相关文章

vue 实现图片单选

vue 实现图片单选

Vue 实现图片单选功能 在 Vue 中实现图片单选功能,可以通过结合 v-model 和自定义事件来实现。以下是具体实现方法: 方法一:使用 v-model 和计算属性 创建一个 Vue 组件…

vue实现弹窗功能

vue实现弹窗功能

实现基础弹窗组件 在Vue中创建弹窗通常需要定义一个组件,利用v-if或v-show控制显示隐藏。以下是一个基础弹窗组件的实现: <template> <div class="…

vue图片实现功能

vue图片实现功能

图片上传功能实现 使用 <input type="file"> 结合 Vue 的 v-on:change 事件监听文件选择 <template> <input ty…

vue的功能实现

vue的功能实现

Vue 核心功能实现 Vue.js 是一个渐进式 JavaScript 框架,其核心功能主要包括数据绑定、组件化、虚拟 DOM 和响应式系统。以下从不同层面解析其实现原理: 响应式系统 Vue 2.…

vue实现单选状态

vue实现单选状态

实现单选状态的方法 在Vue中实现单选状态通常可以通过以下几种方式完成,具体取决于使用场景和需求。 使用v-model绑定单选按钮 通过v-model可以轻松实现单选按钮的选择状态绑定。将同一组单选…

vue分类功能实现

vue分类功能实现

Vue分类功能实现方法 使用v-for指令渲染分类列表 在Vue模板中使用v-for循环渲染分类数据,结合v-bind动态绑定分类ID或其他属性 <div v-for="category in…