当前位置:首页 > 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绑定选中值:

vue实现单选答题功能

<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
  }
}

在模板中添加进度显示:

vue实现单选答题功能

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

添加样式反馈

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

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

在提交后显示结果:

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实现拨号功能的方法 使用HTML5的tel协议 在Vue模板中直接使用<a>标签的tel:协议实现拨号功能。这种方法适用于移动端浏览器,点击后会调起系统的拨号界面。 <tem…

vue实现审核功能

vue实现审核功能

实现审核功能的基本思路 审核功能通常涉及状态管理、权限控制和操作记录。Vue中可以通过组件化设计、状态管理库(如Vuex或Pinia)和后端API配合实现。 审核状态管理 使用Vuex或Pinia…

vue 实现筛选功能

vue 实现筛选功能

实现筛选功能的基本思路 在Vue中实现筛选功能通常涉及以下几个核心步骤:数据绑定、筛选逻辑处理、结果展示。以下是具体实现方法。 数据准备与绑定 准备一个包含所有数据的数组,并使用v-model或v…

vue实现模态功能

vue实现模态功能

Vue 实现模态框功能 方法一:使用组件和v-if/v-show控制显示 创建独立的模态框组件(如Modal.vue),通过props接收标题、内容等数据,使用v-if或v-show控制显示状态。…

php秒杀功能的实现

php秒杀功能的实现

秒杀功能的核心设计 高并发场景下秒杀系统的核心在于解决超卖问题和性能瓶颈。需要结合缓存、队列、锁机制等技术实现。 数据库设计 商品表需包含库存字段,例如: CREATE TABLE `seckil…

vue 实现组单选

vue 实现组单选

Vue 实现单选组 在 Vue 中实现单选组可以通过多种方式,以下是几种常见的实现方法: 使用 v-model 绑定单选按钮组 通过 v-model 绑定一个变量,单选按钮的 value 属性对应…