wx:for,获取选中的行的数据
发布于 5 年前 作者 jing65 12618 次浏览 来自 问答

代码如下所示

<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 回复

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

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

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

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

回到顶部