獲取radio值的方法:
func:function(e){
var val=e.detail.value;//獲取radio值,類型:字符串
var val2=parseInt(val);//將字符串轉換為number
}
實例:
laternext: function (e){
// console.log(e);
var val=e.detail.value;//獲取radio值,類型:字符串
var val2=parseInt(val);//將字符串轉換為number
var score2 = this.data.score;
score2 += val2;
this.setData({
score: score2
})
// console.log(score2);
setTimeout(this.next,500);
},
我遇到的情況:在單選按鈕radio被選中時就刷新選項,設置0.5秒延遲觀感會更好,有0.5秒延遲用戶才會反應過來自己剛才選了哪一個選項。
如果你想延遲一定時間再執行某一函數,就能用到這個定時器了!
setTimeout(方法,時間間隔 毫秒ms);
wxml:
<!--pages/page02/page02.wxml-->
<!-- <text>pages/page02/page02.wxml</text> -->
<view id="bg">
<progress percent="{{pro}}" show-info></progress>
<view id="inside">
<text id="question">{{titles[index]}}</text>
<radio-group bindchange="laternext">
<view id="rad">
<radio value="{{selectA[index].value}}" checked="{{ck}}">{{selectA[index].name}}</radio>
<radio value="{{selectB[index].value}}" checked="{{ck}}">{{selectB[index].name}}</radio>
<radio value="{{selectC[index].value}}" checked="{{ck}}">{{selectC[index].name}}</radio>
</view>
</radio-group>
</view>
</view>
部分js代碼
next: function () {
var index2 = this.data.index;
index2++;
var score2 = this.data.score;
var pro2 = this.data.pro;
pro2 = (index2/8)*100;
if (index2 == 8) {
var app=getApp();
app.data.scoresum = this.data.score;
console.log(app.data.scoresum);
wx: wx.redirectTo({
url: '../page03/page03',
})
return false;
}
this.setData({
index: index2,
ck: false,
pro : pro2
})
},
laternext: function (e){
// console.log(e);
var val=e.detail.value;
var val2=parseInt(val);
var score2 = this.data.score;
score2 += val2;
this.setData({
score: score2
})
// console.log(score2);
setTimeout(this.next,500);
},
為什么綁定事件 bindchange=“laternext”,而不直接綁定next?
如果直接綁定next,不寫laternext函數,將e.detai.value獲取值的語句寫在next中,然后setTimeout(this.next,500),這樣e是未定義undefined的,也就得不到選項的值,所以必須把獲取值寫在laternext函數里,再用setTimeout(next方法,時間間隔 毫秒ms);