当前位置:首页 > VUE

vue实现多选答题

2026-01-08 14:55:18VUE

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实现多选联动的方法 使用v-model绑定数组 在Vue中,可以通过v-model绑定一个数组来实现多选联动。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue为啥能实现监听

vue为啥能实现监听

Vue 实现监听的原理 Vue 的监听能力主要基于其响应式系统,该系统通过数据劫持和依赖收集实现。当数据变化时,系统能自动触发视图更新或执行回调函数。 核心机制:Object.defineProp…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 EC…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独…