当前位置:首页 > 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实现前端打印功能 使用window.print()方法 Vue中可以直接调用浏览器的window.print()方法实现打印功能。这种方法简单快捷,适用于打印整个页面或特定区域。 meth…

vue实现轨道功能

vue实现轨道功能

Vue 实现轨道功能的方法 轨道功能通常指在界面中实现元素沿特定路径移动的效果,例如进度条、时间轴或动画轨迹。以下是几种常见的实现方式: 使用 CSS 动画与 Vue 结合 通过 Vue 动态绑定样…

vue实现复选功能

vue实现复选功能

Vue 实现复选功能 基础实现 使用 v-model 绑定到数组实现多选功能。当复选框被选中时,其 value 会被添加到数组中;取消选中时,会从数组中移除。 <template>…

php实现评论功能

php实现评论功能

数据库设计 创建评论表(示例结构): CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id`…

php购物车功能实现

php购物车功能实现

数据库设计 购物车功能通常需要设计数据库表来存储商品和用户信息。常见的表包括: products 表:存储商品信息,如商品ID、名称、价格、库存等。 users 表:存储用户信息,如用户ID、用户名…

vue打印功能实现

vue打印功能实现

使用Vue实现打印功能 Vue中实现打印功能可以通过浏览器原生的window.print()方法或借助第三方库如vue-print-nb来实现。以下是两种常见方法: 方法一:使用原生JavaScri…