本文共 877 字,大约阅读时间需要 2 分钟。
wxml代码
info="{ {item.num==0?'':item.num}}"
为三元运算符,当{ {item.num}}
为0是不显示,不为0时显示data-index='{ {index}}'
为传过去的数据索引值
data中的数据
data:{ goodsList: [ { _id:1, title: "澳洲原切牛排", desc: "一场精致的烛光晚餐从它开始", price: "8.00", imgUrl: "../../static/images/rou.png", num:0 // 需要改变的数据 }, { _id:2, title: "安吉散养土鸡蛋", desc: "鲜嫩的口味,把田园的清新带给你", price: "8.00", imgUrl: "../../static/images/dan.png", num: 1 // 需要改变的数据 } ]}
数组对象中的
num
为需要修改的值
js函数代码
add:function(e){ let id = e.currentTarget.dataset.id // 获取传入的id let index = e.currentTarget.dataset.index // 获取数据的索引 let temp = 'goodsList[' + index +'].num' // 获取goodsList[index].num this.setData({ [temp]:this.data.goodsList[index].num + 1 }) }
let temp = 'goodsList[' + index +'].num'
为主要代码,打印出的数据为goodsList[index].num
注:index为变量,根据传入的索引值而改变
转载地址:http://vmad.baihongyu.com/