小程序for循环绑定每组数据的id,并通过id获取里面某个数组的值的方法
WXML:
<block wx:for="{{nums}}" wx:for-index='key' wx:for-item='item'> <view class="maxbox" bindtap="imgchange" id="{{key}}"> <text class="nums">{{item.num}}</text> </view> </block>
JS:
Page({ data: { nums:[ {num:11}, {num:22} ] }, imgchange: function (event) { //获取当前item的下标id 通过currentTarget.id var id = event.currentTarget.id; console.log(id); var nums = this.data.nums[id].num; console.log(nums); }, })