当前位置:首页 > 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>

脚本部分

<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] || "";
    }
  }
}

添加进度显示

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

<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实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Car…

vue实现皮肤切换

vue实现皮肤切换

实现皮肤切换的基本思路 在Vue中实现皮肤切换功能,通常涉及动态修改CSS变量或类名。核心是通过状态管理当前主题,并在组件中应用对应的样式。 使用CSS变量实现主题切换 CSS变量(自定义属性)是实…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…