当前位置:首页 > 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 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template> &…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue监听实现

vue监听实现

Vue 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…