首页/知天下事/正文
手把手教你搭建答题活动小程序:实现完整答题交互逻辑与功能

 2025年09月05日  阅读 6

摘要:答题交互逻辑实现在上一篇文章中,我们已经完成了题目数据的获取和动态渲染,现在需要实现完整的答题交互流程。首先在wxml文件中,我们需要使用radio-group组件来展示选项,并绑定change事件。<radio-groupbindchange=...

答题交互逻辑实现

在上一篇文章中,我们已经完成了题目数据的获取和动态渲染,现在需要实现完整的答题交互流程。首先在wxml文件中,我们需要使用radio-group组件来展示选项,并绑定change事件。


<radio-group bindchange="handleAnswerChange">
  <block wx:for="{{currentQuestion.options}}" wx:key="index">
    <radio value="{{item}}">{{item}}</radio>
</radio-group>
<b>答案选择与存储</b>
在对应的js文件中,我们需要定义handleAnswerChange函数来处理用户选择。这个函数会将用户选择的答案暂时存储在页面data中:
handleAnswerChange: function(e) {
  this.setData({
    selectedAnswer: e.detail.value
  });
}
<b>下一题切换逻辑</b>
要实现下一题功能,首先需要在wxml中添加一个"下一题"按钮:
<button bindtap="nextQuestion">下一题</button>
然后在js文件中定义nextQuestion函数:
nextQuestion: function() {
  if (!this.data.selectedAnswer) {
    wx.showToast({
      title: '请选择一个答案',
      icon: 'none'
    });
    return;
  }
  
  // 判断答案对错逻辑
  const isCorrect = this.data.selectedAnswer === this.data.currentQuestion.answer;
  // 保存用户答案
  const userAnswers = this.data.userAnswers;
  userAnswers.push({
    questionId: this.data.currentQuestion._id,
    answer: this.data.selectedAnswer,
    isCorrect: isCorrect
  });
  
  // 更新当前题号
  const nextIndex = this.data.currentIndex + 1;
  if (nextIndex < this.data.questions.length) {
    this.setData({
      currentIndex: nextIndex,
      currentQuestion: this.data.questions[nextIndex],
      selectedAnswer: '',
      userAnswers: userAnswers
    });
  } else {
    // 如果是最后一题则提交答卷
    this.submitAnswers();
  }
}

答卷提交功能

实现submitAnswers函数来提交用户的全部答案到云端:

submitAnswers: function() {

const db = wx.cloud.database();

const score = this.data.userAnswers.filter(item => item.isCorrect).length;

db.collection('user_answers').add({

data: {

微信小程序云开发数组怎么活_云开发答题逻辑_答题小程序开发

userId: getApp().globalData.userInfo._id,

answers: this.data.userAnswers,

score: score,

createTime: db.serverDate()

},

success: res => {

wx.redirectTo({

url: '/pages/result/result?id=' + res._id

});

// 选中选项事件
  radioChange(e){
    this.data.chooseValue[this.data.index] = e.detail.value;
  },

fail: err => {

console.error(err);

wx.showToast({

title: '提交失败',

icon: 'none'

});

}

});

}

nextSubmit(){
    // 如果没有选择
    if (this.data.chooseValue[this.data.index] == undefined || this.data.chooseValue[this.data.index].length == 0) {  
      return wx.showToast({
        title: '请选择答案!',
        icon: 'none',
        duration: 2000
      })
    }
    // 判断所选择的选项是否为正确答案
    this.chooseJudge();
    // 判断是不是最后一题
    this.lastJudge();
  },

`

自动评分与结果跳转

在上面的submitAnswers函数中,我们已经实现了自动评分功能,通过统计正确答案数量得到分数。提交成功后,会跳转到结果页面并传递本次答题记录的ID。

完整流程总结

1. 用户选择答案后,答案被存储在页面data中

// 判断所选择的选项是否为正确答案
  chooseJudge(){
    var trueValue = this.data.questionList[this.data.index]['true'];
    var chooseVal = this.data.chooseValue[this.data.index];
    if (chooseVal.toString() != trueValue.toString()) {
      // 答错则记录错题
      this.data.wrong++;
      this.data.wrongListSort.push(this.data.index);
      this.data.wrongList.push(this.data.questionList[this.data.index]._id);
    }else{
      // 答对则累计总分
      this.setData({
        totalScore: this.data.totalScore + this.data.questionList[this.data.index]['scores']
      })
    }
  },

2. 点击下一题时:

验证用户是否已选择答案

判断答案对错并记录

如果是最后一题则提交答卷

// 判断是不是最后一题
  lastJudge(){
    if (this.data.index < this.data.questionList.length - 1) {
      // 如果不是最后一题,则切换下一题
      let index = this.data.index + 1;
      this.setData({
        index
      })
    } else {
      // 如果是最后一题,则提交答卷
      this.addExamRecord()
    }
  },

否则更新到下一题

3. 提交答卷时:

计算得分

将全部答案和成绩保存到云端

跳转到结果页面

至此,我们已经完成了完整的答题交互逻辑实现,包括答案选择、对错判断、自动评分、答卷提交等功能。这些功能共同构成了一个完整的答题流程前端实现。下一篇文章将详细介绍如何在结果页面展示成绩和答题详情。

// 提交答卷
  addExamRecord(){
    wx.showLoading({
      title: '提交答卷中'
    });
    let examResult = {
      wrongList: this.data.wrongList,
      wrong: this.data.wrong,
      wrongListSort: this.data.wrongListSort,
      chooseValue: this.data.chooseValue,
      totalScore: this.data.totalScore
    };
    activityRecord.add({
      data: {
        ...examResult,
        createDate: db.serverDate()
      }
    }).then(res => {
      // 跳转到答题结果页,查看成绩
      wx.redirectTo({
        url: '../results/results'
      });
      wx.hideLoading();
    })
  }

版权声明:本文为 “博览广文网” 原创文章,转载请附上原文出处链接及本声明;

原文链接:http://wen.bjhwtx.com/post/38240.html

标签:

博览广文网

博览广文网为所有文学爱好者、新闻爱好者、关注生活多方面内容的观众朋友提供多方位的内容呈现、提升阅读空间、填充碎片时间,开阔读者的视野、增长见识、了解民生、一个让您不出户尽知天下事的网站平台!
热门标签
关于我们
广文舒阅网—让天下读者有家可归!这里汇聚了各类优质文化信息,无论是全球热点、历史故事,还是实用百科、趣味探索,您都能轻松获取。我们希望用阅读点亮您的世界,让每一次浏览都充满收获和乐趣。
导航栏A标题
广文舒阅网
扫码关注
联系方式
全国服务热线:0755-88186625
Q Q:8705332
Email:admin@lanyu.com
地址:深圳市福田区海雅缤纷国际大厦5层501
Copyright 深圳市蓝宇科技有限公司 版权所有 备案号:京ICP备20013102号-1