鸿蒙中JS页面组件属性值修改后,页面没有刷新

利用stepper组件做了个问卷调查,每道题目有四个选项用了四个radio组件,想做一个功能,就是当用户点击上一步或者下一步时,利用checked属性可以显示之前选过的答案,但只有点击上一步时可以正确显示,在查看之前的题目就无法显示。请问哪里出错了。反复检查了四个radio组件的checked属性值都是对的。

页面代码如下:

<div class="container">
    <stepper  @change="switchStepper" onfinish="finishEvent">
        <stepper-item for="{{questions}}" label="{{labelName}}">
            <div class="stepper-item">
                <text  class="title">共{{questions.length}}题,当前第{{$idx+1}}题</text>
                <text class="detail" >{{$item.detail}}</text>
                <div>
                    <input type="radio" name="tiType" value="{{$idx+'A'}}" @change="selectAnswer({{$idx+'A'}})" checked="{{questions[$idx].answers[0]}}" id="one"></input>
                    <text class="option-css">{{$item.optionA}}</text>
                </div>
                <div>
                    <input type="radio" name="tiType" value="{{$idx+'B'}}" @change="selectAnswer({{$idx+'B'}})" checked="{{questions[$idx].answers[1]}}" id="two"></input>
                    <text class="option-css">{{$item.optionB}}</text>
                </div>
                <div>
                    <input type="radio" name="tiType" value="{{$idx+'C'}}" @change="selectAnswer({{$idx+'C'}})" checked="{{questions[$idx].answers[2]}}" id="three"></input>
                    <text class="option-css">{{$item.optionC}}</text>
                </div>
                <div>
                    <input type="radio" name="tiType" value="{{$idx+'D'}}" @change="selectAnswer({{$idx+'D'}})" checked="{{questions[$idx].answers[3]}}" id="four"></input>
                    <text class="option-css">{{$item.optionD}}</text>
                </div>
            </div>
        </stepper-item>
    </stepper>
</div>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

js代码如下:

export default {
    data: {
        questions:[
            {
                detail:"入睡时间",
                optionA:"没问题",
                optionB:"轻微延迟",
                optionC:"显著延迟",
                optionD:"延迟严重或没有睡觉",
                answers:[false,false,false,false],
                answer:""
            },
            {
                detail:"夜间苏醒",
                optionA:"没问题",
                optionB:"轻微影响",
                optionC:"显著影响",
                optionD:"严重影响或没有睡觉",
                answers:[false,false,false,false],
                answer:""
            },
            {
                detail:"比期望的时间早醒",
                optionA:"没问题",
                optionB:"轻微提早",
                optionC:"显著提早",
                optionD:"严重提早或没有睡觉",
                answers:[false,false,false,false],
                answer:""
            },
            {
                detail:"总睡眠时间",
                optionA:"足够",
                optionB:"轻微不足",
                optionC:"显著不足",
                optionD:"严重不足或没有睡觉",
                answers:[false,false,false,false],
                answer:""
            },
            {
                detail:"总睡眠质量",
                optionA:"满意",
                optionB:"轻微不满",
                optionC:"显著不满",
                optionD:"严重不满或没有睡觉",
                answers:[false,false,false,false],
                answer:""
            },
            {
                detail:"白天情绪",
                optionA:"正常",
                optionB:"轻微低落",
                optionC:"显著低落",
                optionD:"严重低落",
                answers:[false,false,false,false],
                answer:""
            },
            {
                detail:"白天身体功能",
                optionA:"足够",
                optionB:"轻微影响",
                optionC:"显著影响",
                optionD:"严重影响",
                answers:[false,false,false,false],
                answer:""
            },
            {
                detail:"白天思睡",
                optionA:"无思睡",
                optionB:"轻微思睡",
                optionC:"显著思睡",
                optionD:"严重思睡",
                answers:[false,false,false,false],
                answer:""
            }
        ],       
        labelName:{
            prevLabel:"前一题",
            nextLabel:"后一题",
            status:"normal"
        }
    },
    onInit() {

    },
    selectAnswer(inputValue,e){
        if(inputValue == e.value){                    
            let val = e.value;
            let index = val.substring(0,1);
            let answer = val.substring(1,2);
            this.questions[index].answer = answer;
            this.getAnswer(answer,index);
            console.info(this.questions[index].answers);
        }
    },
    getAnswer(answer,index){
        this.questions[index].answers = [false,false,false,false]
        switch(answer){
            case "A": this.questions[index].answers[0] = true;break;
            case "B": this.questions[index].answers[1] = true;break;
            case "C": this.questions[index].answers[2] = true;break;
            case "D": this.questions[index].answers[3] = true;break;
        }

    },
    switchStepper(e){
        console.info("index = " + e.index);    
        console.info("answers = " + this.questions[e.index].answers);

    },
    finishEvent(){
        console.info("finish");        let score = 0;
        for(let i =0;i < this.questions.length;i++){
            switch(this.questions[i].answer){
                case "A":score += 0;break;
                case "B":score += 1;break;
                case "C":score += 2;break;
                case "D":score += 3;break;

            }
        }
        console.info("score = " + score);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.

经过反复检查,​​并在stepper的change事件中利用console.info​​,打印了数组中每个对象的answers的值都是对的。就是没法显示正确的结果比如:

鸿蒙中JS页面组件属性值修改后,页面没有刷新-鸿蒙开发者社区

比如第二题,之前选中的第三个radio,等再次回到第二题的时第三个radio就没法正确显示,但checked的属性值的确是true。

请问如何可以让radio组件的checked属性绑定的数据在修改后,可以更新页面。

HarmonyOS UI组件
2023-02-16 13:24:55
浏览
收藏 0
回答 0


相关问题
【列表数据更新页面刷新
379浏览 • 1回复 待解决
HarmonyOS 页面
834浏览 • 1回复 待解决