HarmonyOS鸿蒙Next中在stepper中无法保存单选框的选项

HarmonyOS鸿蒙Next中在stepper中无法保存单选框的选项 利用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">
                    <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">
                    <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">
                    <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">
                    <text class="option-css">{{$item.optionD}}</text>
                </div>
            </div>
        </stepper-item>
    </stepper>
</div>

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);
    }
}

通过在change事件中打印answers数组里面的boolean值都是对的。但就是页面上的对应的radio显示的是未选中状态。


更多关于HarmonyOS鸿蒙Next中在stepper中无法保存单选框的选项的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

你的change事件写错了,不需要加{{}}的,

更多关于HarmonyOS鸿蒙Next中在stepper中无法保存单选框的选项的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


去掉了还是不行,没法显示之前选择的选项,很奇怪,每个radio的checked的属性值都是对的。就是不显示选中后的状态。

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

onchange最后给answer数组push一下试试,

问题依旧啊。明明checked属性值是对的。就是无法显示正确的结果。

QQ截图20230215124513.png

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

在HarmonyOS鸿蒙Next中,Stepper组件用于分步操作,而单选框(RadioButton)用于单选。如果无法保存单选框的选项,可能是因为状态管理未正确实现。建议在onChange事件中通过@State@Link装饰器保存单选框的选中状态。例如:

@State selectedOption: string = '';

RadioGroup({ onChange: (value) => { this.selectedOption = value; } }) {
  RadioButton({ value: 'Option1' }).text('Option1')
  RadioButton({ value: 'Option2' }).text('Option2')
}

确保在Stepper的每一步中正确传递和更新状态。

回到顶部