当前位置:首页 > VUE

vue实现多选答题

2026-01-08 14:55:18VUE

vue实现多选答题

vue实现多选答题

Vue 实现多选答题功能

实现多选答题功能需要结合 Vue 的数据绑定和事件处理机制,以下是一个完整的实现方案:

数据结构设计

data() {
  return {
    questions: [
      {
        id: 1,
        text: "以下哪些是前端框架?",
        options: [
          { id: 1, text: "Vue", selected: false },
          { id: 2, text: "React", selected: false },
          { id: 3, text: "Angular", selected: false },
          { id: 4, text: "jQuery", selected: false }
        ],
        multiple: true
      }
    ],
    currentQuestionIndex: 0
  }
}

模板渲染

<div v-for="(question, qIndex) in questions" :key="question.id">
  <h3>{{ question.text }}</h3>
  <div v-for="(option, oIndex) in question.options" :key="option.id">
    <label>
      <input 
        type="checkbox" 
        v-model="option.selected"
        @change="handleOptionChange(question, option)"
      >
      {{ option.text }}
    </label>
  </div>
</div>

选项选择处理

methods: {
  handleOptionChange(question, changedOption) {
    if (!question.multiple) {
      question.options.forEach(option => {
        if (option.id !== changedOption.id) {
          option.selected = false
        }
      })
    }
  }
}

答案提交验证

methods: {
  submitAnswers() {
    const selectedOptions = this.questions[this.currentQuestionIndex].options
      .filter(opt => opt.selected)
      .map(opt => opt.id)

    if (selectedOptions.length === 0) {
      alert("请至少选择一个选项")
      return
    }

    // 处理提交逻辑
    console.log("已选答案:", selectedOptions)
  }
}

样式优化

.question-container {
  margin-bottom: 20px;
}

.option-item {
  margin: 10px 0;
  display: flex;
  align-items: center;
}

input[type="checkbox"] {
  margin-right: 10px;
}

.submit-btn {
  margin-top: 20px;
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

完整组件示例

<template>
  <div class="quiz-container">
    <div v-for="(question, qIndex) in questions" 
         :key="question.id" 
         class="question-container">
      <h3>{{ question.text }}</h3>
      <div v-for="(option, oIndex) in question.options" 
           :key="option.id" 
           class="option-item">
        <label>
          <input 
            type="checkbox" 
            v-model="option.selected"
            @change="handleOptionChange(question, option)"
          >
          {{ option.text }}
        </label>
      </div>
    </div>
    <button class="submit-btn" @click="submitAnswers">提交答案</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      questions: [
        {
          id: 1,
          text: "以下哪些是前端框架?",
          options: [
            { id: 1, text: "Vue", selected: false },
            { id: 2, text: "React", selected: false },
            { id: 3, text: "Angular", selected: false },
            { id: 4, text: "jQuery", selected: false }
          ],
          multiple: true
        }
      ]
    }
  },
  methods: {
    handleOptionChange(question, changedOption) {
      if (!question.multiple) {
        question.options.forEach(option => {
          if (option.id !== changedOption.id) {
            option.selected = false
          }
        })
      }
    },
    submitAnswers() {
      const selectedOptions = this.questions[0].options
        .filter(opt => opt.selected)
        .map(opt => opt.id)

      if (selectedOptions.length === 0) {
        alert("请至少选择一个选项")
        return
      }

      console.log("已选答案:", selectedOptions)
    }
  }
}
</script>

<style scoped>
/* 样式代码同上 */
</style>

这个实现方案包含了多选答题的核心功能,可以根据实际需求进行扩展,如添加更多题目、实现题目切换、添加计时功能等。

标签: 多选vue
分享给朋友:

相关文章

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newIte…