wx:for,获取选中的行的数据

发布于 6 年前作者 jing6512835 次浏览最后编辑 6 年前来自 ask

代码如下所示

<view wx:for="{{array}}" >
  <view bindtap="showVisitDetail" style="border-color: blue;">
  <view  hidden="true">{{item.id}}</view>
  {{index}}: {{item.message}}
  </view>
</view>
data: {
  array: [{
    id: 1,
    message: 'foo',
  }, {
    id: 2,
    message: 'bar'
  }]
},
/**
 * 显示选中行的数据
 */
showVisitDetail: function (e) {
  console.log(e);
},

需求是,选中其中一行,获取对应行的数据。

实在没想通。

2 回复
weijun
weijun1 楼6 年前

谢谢,是我理解得不够深刻。

yanluo
yanluo2 楼4 年前

绑定事件那一行增加data-id=“{{item.id}}” data-message=“{{item.message}}”

js里对应方法用e.currentTarget.dataset.id和e.currentTarget.dataset.message获取对应数据

data-后面的字符串可以随便写,dataset.后面的属性对应就好。其实文档里都有