当前位置:首页 > VUE

vue实现答题切换

2026-01-19 08:43:43VUE

实现答题切换功能

在Vue中实现答题切换功能可以通过动态绑定题目数据和切换索引来完成。以下是一个基本实现示例:

模板部分

<template>
  <div class="quiz-container">
    <div v-if="currentQuestion">
      <h3>{{ currentQuestion.text }}</h3>
      <ul>
        <li v-for="(option, index) in currentQuestion.options" :key="index">
          <input 
            type="radio" 
            :id="'option'+index" 
            :value="option" 
            v-model="selectedAnswer"
          >
          <label :for="'option'+index">{{ option }}</label>
        </li>
      </ul>
    </div>

    <button @click="prevQuestion" :disabled="currentIndex === 0">上一题</button>
    <button @click="nextQuestion" :disabled="currentIndex === questions.length - 1">下一题</button>
  </div>
</template>

脚本部分

vue实现答题切换

<script>
export default {
  data() {
    return {
      questions: [
        {
          text: "问题1",
          options: ["选项A", "选项B", "选项C"],
          answer: "选项A"
        },
        {
          text: "问题2", 
          options: ["选项1", "选项2", "选项3"],
          answer: "选项2"
        }
      ],
      currentIndex: 0,
      selectedAnswer: ""
    }
  },
  computed: {
    currentQuestion() {
      return this.questions[this.currentIndex];
    }
  },
  methods: {
    nextQuestion() {
      if (this.currentIndex < this.questions.length - 1) {
        this.currentIndex++;
        this.selectedAnswer = "";
      }
    },
    prevQuestion() {
      if (this.currentIndex > 0) {
        this.currentIndex--;
        this.selectedAnswer = "";
      }
    }
  }
}
</script>

添加答题状态保存

为了在切换题目时保存已选择的答案,可以修改数据结构:

data() {
  return {
    questions: [...],
    currentIndex: 0,
    answers: {} // 使用对象保存每题的答案
  }
},
methods: {
  nextQuestion() {
    // 保存当前答案
    this.$set(this.answers, this.currentIndex, this.selectedAnswer);

    if (this.currentIndex < this.questions.length - 1) {
      this.currentIndex++;
      this.selectedAnswer = this.answers[this.currentIndex] || "";
    }
  },
  prevQuestion() {
    this.$set(this.answers, this.currentIndex, this.selectedAnswer);

    if (this.currentIndex > 0) {
      this.currentIndex--;
      this.selectedAnswer = this.answers[this.currentIndex] || "";
    }
  }
}

添加进度显示

可以在模板中添加进度指示:

vue实现答题切换

<div class="progress">
  当前进度: {{ currentIndex + 1 }}/{{ questions.length }}
</div>

样式优化

添加基础样式提升用户体验:

<style>
.quiz-container {
  max-width: 600px;
  margin: 0 auto;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  margin: 10px 0;
}

button {
  margin: 10px;
  padding: 8px 16px;
}

button:disabled {
  opacity: 0.5;
}

.progress {
  margin: 20px 0;
  font-weight: bold;
}
</style>

完整组件示例

将以上部分组合起来,就形成了一个完整的答题切换组件:

<template>
  <div class="quiz-container">
    <div class="progress">
      当前进度: {{ currentIndex + 1 }}/{{ questions.length }}
    </div>

    <div v-if="currentQuestion">
      <h3>{{ currentQuestion.text }}</h3>
      <ul>
        <li v-for="(option, index) in currentQuestion.options" :key="index">
          <input 
            type="radio" 
            :id="'option'+index" 
            :value="option" 
            v-model="selectedAnswer"
          >
          <label :for="'option'+index">{{ option }}</label>
        </li>
      </ul>
    </div>

    <button @click="prevQuestion" :disabled="currentIndex === 0">上一题</button>
    <button @click="nextQuestion" :disabled="currentIndex === questions.length - 1">下一题</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      questions: [
        {
          text: "Vue是什么?",
          options: ["前端框架", "后端语言", "数据库"],
          answer: "前端框架"
        },
        {
          text: "v-model的作用是什么?", 
          options: ["数据绑定", "样式绑定", "事件绑定"],
          answer: "数据绑定"
        },
        {
          text: "Vue的核心特性不包括?",
          options: ["组件化", "响应式", "多线程"],
          answer: "多线程"
        }
      ],
      currentIndex: 0,
      answers: {},
      selectedAnswer: ""
    }
  },
  computed: {
    currentQuestion() {
      return this.questions[this.currentIndex];
    }
  },
  methods: {
    nextQuestion() {
      this.$set(this.answers, this.currentIndex, this.selectedAnswer);

      if (this.currentIndex < this.questions.length - 1) {
        this.currentIndex++;
        this.selectedAnswer = this.answers[this.currentIndex] || "";
      }
    },
    prevQuestion() {
      this.$set(this.answers, this.currentIndex, this.selectedAnswer);

      if (this.currentIndex > 0) {
        this.currentIndex--;
        this.selectedAnswer = this.answers[this.currentIndex] || "";
      }
    }
  }
}
</script>

<style>
.quiz-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  margin: 10px 0;
}

button {
  margin: 10px;
  padding: 8px 16px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.progress {
  margin: 20px 0;
  font-weight: bold;
}

input[type="radio"] {
  margin-right: 10px;
}
</style>

这个实现包含了答题切换的基本功能,可以进一步扩展添加提交验证、计时器、答题结果统计等高级功能。

标签: vue
分享给朋友:

相关文章

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keyword…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue 实现文字滚动

vue 实现文字滚动

Vue 实现文字滚动效果 使用CSS动画实现 通过CSS的@keyframes和transform属性实现文字滚动效果: <template> <div class="scro…

vue实现点击跳转

vue实现点击跳转

Vue 实现点击跳转的方法 在 Vue 中实现点击跳转可以通过以下几种方式: 使用 router-link router-link 是 Vue Router 提供的组件,用于声明式导航。适合在模板中…

vue实现前端录制

vue实现前端录制

Vue 实现前端录制功能 前端录制通常包括音频、视频或屏幕录制,结合Vue框架可以通过浏览器API实现。以下是几种常见录制场景的实现方法: 音频录制 使用浏览器MediaRecorder API实现…