当前位置:首页 > VUE

vue实现多选答题

2026-01-08 14:55:18VUE

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 实现图片分析的方法 在 Vue 中实现图片分析通常涉及图片上传、预览、后端交互以及分析结果展示等功能。以下是几种常见的实现方式: 使用 HTML5 File API 和 Canvas…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些…

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来…