首页 > 技术文章 > 微信小程序单选按钮radio选中的值value的获取方法,setTimeout定时器的用法

BIG-BOSS-ZC 2019-07-16 20:35 原文

获取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);

推荐阅读