小程序获取各种数据值跟设置数据值

来源:互联网 发布:初中毕业能学php吗 编辑:程序博客网 时间:2024/05/22 17:02

修改设置data数组中的某个值:

//小程序接收后台数据若为数组时,下标最好为默认索引数组,从0开始排序 var articleId ='1'//对应数组中的索引值 var thisArticle = "articleList[" + articleId + "].iscollection"     console.log(thisArticle);     that.setData({     //必须加[],        [thisArticle]: ishow      })     //若为索引值为定值:egarticleList[0].iscollection,则直接写,不用加[],必须加""      that.setData({       "egarticleList[0].iscollection": ishow      })

小程序修改本页面data中数组中的某个值

//小程序data中的某个值若为数组时,下标最好为**关联**数组Page({  data: {    teacherInfo:{      rachel1:{        name:"rachel",        decoration: "加拿大人,两年多的线上英语教学经验友善耐心,为人随和。擅长烹饪喜欢影视音乐、跳舞更多",        audioImg: "/ico_play.png",        headImg:"/teacher.jpg",        audioSrc:"/001.mp3",      },      rachel2:{        name: "rachel",        decoration: "加拿大人,两年多的线上英语教学经验友善耐心,为人随和。擅长烹饪喜欢影视音乐、跳舞更多",        audioImg: "/ico_play.png",        headImg: "/teacher.jpg",        audioSrc: "/230.mp3",      },    },  },  play:function(event){      var that = this;      var id = event.currentTarget.dataset.id;      console.log(id);      var thisTeacher_autioImg = "teacherInfo."+id+".audioImg";      console.log(thisTeacher_autioImg);      //解析:      thisTeacher_autioImg 若写为这种[]格式("teacherInfo[" + id+ "].audioImg"),则直接修改整个数组,因为[]中只能是数字,若为"teacherInfo[3].audioImg",则teacherInfo数组变为4个值:      teacherInfo:{0:null,1:null,2:null,3:{audioImg:"/ico_playing.png"}}      //解析结束      that.setData({          [thisTeacher_autioImg]: "/ico_playing.png",          playStatus: "pause",        })    },   });

获取数组中某个变量的值:

 var id = event.currentTarget.dataset.id; var a = that.data.teacherInfo[id].decoration;

获取自定义属性 data-id 的值:

event.currentTarget.dataset.id

小程序获取页面传值id 的值:

onLoad: function (options) {    var that = this    var articleId = options.id//获取文章的id值  },

小程序获取input框输入:

wxml:

<input type="number"  placeholder="请输入手机号" bindinput="bindPhone" maxlength="11"/>

js:

bindPhone:function(e){      this.setData({        phone:e.detail.value      })  },
原创粉丝点击