当前位置:首页 > VUE

vue实现各种问卷

2026-02-19 18:57:17VUE

Vue 实现问卷系统的方法

基础问卷表单结构

使用 Vue 的 v-model 绑定表单数据,结合 v-for 动态渲染问题列表。示例代码:

<template>
  <div v-for="(question, index) in questions" :key="index">
    <label>{{ question.text }}</label>
    <input 
      v-if="question.type === 'text'"
      v-model="answers[index]"
      type="text"
    >
    <select v-else-if="question.type === 'select'" v-model="answers[index]">
      <option v-for="opt in question.options" :value="opt">{{ opt }}</option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      questions: [
        { text: "姓名", type: "text" },
        { text: "满意度", type: "select", options: ["满意", "一般", "不满意"] }
      ],
      answers: []
    }
  }
}
</script>

动态添加问题

通过数组方法实现问题管理:

methods: {
  addQuestion() {
    this.questions.push({
      text: "",
      type: "text",
      options: []
    });
  },
  removeQuestion(index) {
    this.questions.splice(index, 1);
  }
}

表单验证

使用 Vue 的 computed 属性或第三方库如 VeeValidate:

vue实现各种问卷

computed: {
  isFormValid() {
    return this.answers.every(answer => answer !== "");
  }
}

保存问卷数据

通过 API 调用或本地存储:

methods: {
  submitForm() {
    axios.post('/api/survey', { answers: this.answers })
      .then(response => {
        alert('提交成功');
      });
  }
}

可视化结果展示

结合图表库如 ECharts 展示统计结果:

vue实现各种问卷

<template>
  <div ref="chart" style="width: 600px; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  mounted() {
    const chart = echarts.init(this.$refs.chart);
    chart.setOption({
      xAxis: { data: ['满意', '一般', '不满意'] },
      yAxis: {},
      series: [{ data: [30, 20, 10], type: 'bar' }]
    });
  }
}
</script>

进阶功能实现

  1. 条件逻辑跳转:通过 watch 监听答案变化

    watch: {
    answers: {
     handler(newVal) {
       if (newVal[0] === '不满意') {
         this.showFollowUp = true;
       }
     },
     deep: true
    }
    }
  2. 多页问卷:使用动态组件或路由分页

    <component :is="currentPageComponent" />
  3. 拖拽排序:集成 vuedraggable

    <draggable v-model="questions">
    <div v-for="(item, index) in questions" :key="index">
     {{ item.text }}
    </div>
    </draggable>

注意事项

  • 复杂问卷建议使用专业表单库如 vue-form-generator
  • 移动端适配需考虑触摸事件和响应式布局
  • 数据持久化建议结合本地存储或数据库
  • 性能优化可使用虚拟滚动处理长列表

标签: 问卷vue
分享给朋友:

相关文章

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue实现爬虫

vue实现爬虫

Vue 实现爬虫的基本思路 Vue.js 本身是一个前端框架,主要用于构建用户界面。要实现爬虫功能,通常需要结合后端技术或浏览器自动化工具。以下是几种常见的方法: 方法一:Vue + Node.js…