html52.com
我爱小程序-开发者交流社区

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

问题模块
API和组件

代码如下所示

<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);
},

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

实在没想通。


网友回复

醇儿:

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

周彬:

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

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

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

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付