如何比较result和answer并用scoer算出总分?一直提示Setting data field "score" to undefined is invalid.并且结果页面的scoer显示为零。
// pages/result/result.js
Page({
/**
* 页面的初始数据
*/
data: {
score:0,
},
anew:function(e){
wx.reLaunch({
url: '/pages/test/test',
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
var uname = wx.getStorageSync('uname')
this.setData({
uname:uname,
score:options.score,
})
},
})
<view class="page_result">
<button class="new" bindtap="anew">重新测试button>
<view class="score">{{uname}}你的总分为:{{score}}view>
view>
// pages/test/test.js
Page({
/**
* 页面的初始数据
*/
data: {
index:1,
uname:app.globalData.uname,
scoer:0,
userResult:[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
currentIndex:0,
questionList:[
{
q:"1+1=",
item:["1","2","3","4"],
answer:1,
},
{
q:"1+2=",
item:["1","2","3","4"],
answer:2,
},
{
q:"1+3=",
item:["1","2","3","4"],
answer:3,
},
{
q:"1+5=",
item:["6","7","8","9"],
answer:0,
},
{
q:"1+6=",
item:["6","7","8","9"],
answer:1,
},
{
q:"1+7=",
item:["6","7","8","9"],
answer:2,
},
{
q:"1+8=",
item:["6","7","8","9"],
answer:3,
},
{
q:"1+9=",
item:["6","10","8","9"],
answer:1,
},
{
q:"9+1=",
item:["6","7","10","9"],
answer:2,
},
],
},
OnItemChange:function(e){
let result = this.data.userResult
result[this.data.currentIndex] = parseInt(e.detail.value)
let questionList = this.data.questionList;
for (let i = 0; i < result.length; i++) {
for (let j = 0; j < questionList.length; j++) {
if(result[i] == questionList[j].answer){
this.setData({
scoer:this.data.scoer+1
})
}
}
}
},
last:function(e){
if(this.data.index>1){
this.setData({
currentIndex:this.data.currentIndex-1
})
}else{
wx.showModal({
title: '提示',
content: '已是第一题',
})
}
},
next:function(e){
if(this.data.index1<10){
this.setData({
currentIndex:this.data.currentIndex+1
})
}else{
wx.showModal({
title: '提示',
content: '已是最后一题',
})
}
},
submit:function(e){
wx.reLaunch({
url:'/pages/result/result?scoer='+this.data.scoer
})
console.log(e)
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
var uname = wx.getStorageSync('uname')
this.setData({
uname:uname
})
var that = this;
var tscoer = this.data.scoer;
this.setData({
tscoer:tscoer,
})
},
})
<view class="page">
<view>用户:{{uname}}view>
<view>{{questionList[currentIndex].q}}view>
<radio-group name="change" bindchange="OnItemChange">
<view class="op">
<radio value='{{index}}' wx:for="{{questionList[currentIndex].item}}" wx:key="key">{{item}}radio>
view>
radio-group>
<button bindtap="last">上一题button>
<button bindtap='submit'>提交button>
<button bindtap='next'>下一题button>
view>
<view>
<view>用户:{{uname}}</view>
<view>{{questionList[currentIndex].q}}</view>
<radio-group name="change" bindchange="OnItemChange">
<view class="op">
<radio wx:for="{{questionList[currentIndex].item}}" wx:key="key"value='{{item}}'>{{item}}</radio>
</view>
</radio-group>
<button bindtap="last">上一题</button>
<button bindtap='submit'>提交</button>
<button bindtap='next'>下一题</button>
</view>
data: {
uname:app.globalData.uname,
scoer:0,
currentIndex:0,
questionList:[
{
q:"1+1=",
item:[1,2,3,4],
answer:2,
},
{
q:"1+2=",
item:[1,2,3,4],
answer:3,
},
{
q:"1+3=",
item:[1,2,3,4],
answer:4,
},
{
q:"1+5=",
item:[6,7,8,9],
answer:6,
},
{
q:"1+6=",
item:[6,7,8,9],
answer:7,
},
{
q:"1+7=",
item:[6,7,8,9],
answer:8,
},
{
q:"1+8=",
item:[6,7,8,9],
answer:9,
},
{
q:"1+9=",
item:[7,8,9,10],
answer:10,
},
{
q:"9+1=",
item:[7,8,9,10],
answer:10,
},
],
},
OnItemChange:function(e){
let result = parseInt(e.detail.value)
let questionList = this.data.questionList;
var scr = this.data.scoer + 1
if(result == questionList[currentIndex].answer){
this.setData({
scoer:scr
})
}else{
this.setData({scoer:scr - 1})
}
},
last:function(e){
var cri = this.data.currentIndex
if(this.data.currentIndex>1){
this.setData({
currentIndex:cri - 1,
})
}else{
wx.showModal({
title: '提示',
content: '已是第一题',
});return
}
},
next:function(e){
var cri = this.data.currentIndex
if(this.data.currentIndex<10){
this.setData({
currentIndex:cri + 1,
})
}else{
wx.showModal({
title: '提示',
content: '已是最后一题',
});return
}
},
submit:function(e){
wx.reLaunch({
url:'/pages/result/result?scoer='+this.data.scoer
})
console.log(e)
},