收藏
回答

如何将全局变量中数组的每一个值依次赋值给page中的data,从而更新其初始数值?

我是小白,在切换页面时想让多个单选框组保留选中状态,于是将e.detail.value存储到全局变量数组中,
然后在onLoad函数中用this.setData依次赋值多个单选问题的value,然后判断value决定是否checked。
却发现第一个单选问题的value值是初始状态的赋值,全局变量无法赋值到page的data中;但是后面几个问题的value又赋值成功。
请教各位有什么简介易懂的方法,让第一个单选问题的value值能被全局变量赋值成功,或者实现切换页面时单选框组保留选中状态。
以下是部分js文件代码:
// 获取应用实例
const app = getApp()
//初始化
Page({
    data: {
        lastAnswers: ['?','?','?','?','?','?'],
        items1: [
            {value: "1", name: '犬或者猫'},
            {value: "2", name: '其他'}
        ],
        ans1: '?',
        items2: [
            {value: "1", name: '一只'},
            {value: "2", name: '多于一只'}
        ],
        ans2: '?',
        items3: [
            {value: "1", name: '指定国家或地区'},
            {value: "2", name: '非指定国家或地区'}
        ],
        ans3: '?',
        items4: [
            {value: "1", name: '能'},
            {value: "2", name: '否'}
        ],
        ans4: '?',
        items5: [
            {value: "1", name: '能'},
            {value: "2", name: '否'}
        ],
        ans5: '?',
        items6: [
            {value: "1", name: '是'},
            {value: "2", name: '否'}
        ],
        ans6: '?',
        result: '',
        hidRes: "true",
    },
    onReady: function(){
        wx.setNavigationBarTitle({
          title: '宠物入境指南'
        })
    },
    onLoad: function(){
        this.setData({
            lastAnswers: app.globalData.module1Answers,
            ansl: app.globalData.module1Answers[0],
            ans2: app.globalData.module1Answers[1],
            ans3: app.globalData.module1Answers[2],
            ans4: app.globalData.module1Answers[3],
            ans5: app.globalData.module1Answers[4],
            ans6: app.globalData.module1Answers[5],
        });
        console.log(this.data.lastAnswers);
        console.log(this.data.ans1);/*bug:ans1还是"?"而不是1或者2,但是ans2&3&4&5&6没有问题*/
        if (this.data.lastAnswers[0] == 1) {
            this.setData({
                items1: [
                    {value: "1", name: '犬或者猫', checked:'true'},
                    {value: "2", name: '其他'}
                ]
            })
        }else if (this.data.lastAnswers[0] == 2) {
            this.setData({
                items1: [
                    {value: "1", name: '犬或者猫'},
                    {value: "2", name: '其他', checked:'true'}
                ]
            })
        };
        if (this.data.lastAnswers[1] == 1) {
            this.setData({
                items2: [
                    {value: "1", name: '一只', checked: 'true'},
                    {value: "2", name: '多于一只'}
                ]
            })
        }else if (this.data.lastAnswers[1] == 2) {
            this.setData({
                items2: [
                    {value: "1", name: '一只'},
                    {value: "2", name: '多于一只', checked: 'true'}
                ]
            })
        };
        if (this.data.lastAnswers[2] == 1) {
            this.setData({
                items3: [
                    {value: "1", name: '指定国家或地区', checked: 'true'},
                    {value: "2", name: '非指定国家或地区'}
                ]
            })
        }else if (this.data.lastAnswers[2] == 2) {
            this.setData({
                items3: [
                    {value: "1", name: '指定国家或地区'},
                    {value: "2", name: '非指定国家或地区', checked: 'true'}
                ]
            })
        };
        if (this.data.lastAnswers[3] == 1) {
            this.setData({
                items4: [
                    {value: "1", name: '能', checked: 'true'},
                    {value: "2", name: '否'}
                ]
            })
        }else if (this.data.lastAnswers[3] == 2) {
            this.setData({
                items4: [
                    {value: "1", name: '能'},
                    {value: "2", name: '否', checked: 'true'}
                ]
            })
        };
        if (this.data.lastAnswers[4] == 1) {
            this.setData({
                items5: [
                    {value: "1", name: '能', checked: 'true'},
                    {value: "2", name: '否'}
                ]
            })
        }else if (this.data.lastAnswers[4] == 2) {
            this.setData({
                items5: [
                    {value: "1", name: '能'},
                    {value: "2", name: '否', checked: 'true'}
                ]
            })
        };
        if (this.data.lastAnswers[5] == 1) {
            this.setData({
                items6: [
                    {value: "1", name: '是', checked: 'true'},
                    {value: "2", name: '否'}
                ]
            })
        }else if (this.data.lastAnswers[5] == 2) {
            this.setData({
                items6: [
                    {value: "1", name: '是'},
                    {value: "2", name: '否', checked: 'true'}
                ]
            })
        }
    },
    radioChange1(e) {
        this.setData({
          ans1: e.detail.value
        })
    },
    radioChange2(e) {
        this.setData({
          ans2: e.detail.value
        })
    },
    radioChange3(e) {
        this.setData({
          ans3: e.detail.value
        })
    },
    radioChange4(e) {
        this.setData({
          ans4: e.detail.value
        })
    },
    radioChange5(e) {
        this.setData({
          ans5: e.detail.value
        })
    },
    radioChange6(e) {
        this.setData({
          ans6: e.detail.value
        })
    },
    submit: function() {
        let answers = [];
        answers.push(this.data.ans1);
        answers.push(this.data.ans2);
        answers.push(this.data.ans3);
        answers.push(this.data.ans4);
        answers.push(this.data.ans5);
        answers.push(this.data.ans6);
        app.globalData.module1Answers = answers;
        if(answers[0] == '?' || answers[1] == '?' || answers[2] == '?' || answers[3] == '?' || answers[4] == '?' || answers[5] == '?') {
            this.setData({
                result: '提示:请回答所有问题。'/*再次点击单选框时answers[0]依旧是初始值“?”,全局变量没有赋值成功*/
              })
......
回答关注问题邀请回答
收藏
登录 后发表内容