Vant Weapp - 輕量、可靠的小程序 UI 組件庫
van-radio name 是一個字符串,無法傳對象的處理
以及 mpx 多層嵌套 for 循環處理
<viewwx:for="{{questionList}}"wx:for-item="question" // item 重命名wx:for-index="questionIndex" // index 重命名wx:key="question"><view wx:show="{{questionIndex + 1 === showIndex}}" class="hz-card"><text class="hz-question">{{ questionIndex + 1 }}:{{ question.question }} ({{ question.type }}類問題)</text><van-radio-groupvalue="{{ answerList[questionIndex].answerItem }}"bind:change="onChange($event, questionIndex, question.type)"direction="horizontal"><van-radio wx:for="{{question.options}}" wx:key="text" name='{{"score:" + item.score + ",text:" + item.text}}'>{{ item.text }}
// name 給傳一個類似對象的字符串</van-radio></van-radio-group></view></view>
onChange(event, index, type) {// vant-radio 中的 name 是字符串,傳不了對象,這么處理事因為有的答案不同但是得分相同const selectData = event.detail // "score:1,text:藍"this.answerList[index].answerItem = selectDatalet score = selectData.split(",")[0].split(":")[1]; // 把 "score:1,text:藍" 中的 score 取出來this.answerList[index].score = parseInt(score)this.answerList[index].type = type},