在微信小程序按钮的点击事件中传递参数可以使用data-*属性,在点击事件中通过e.currentTarget.dataset来获取参数。

例如:

WXML代码:

<view>
  <button data-id='1' bindtap='handleClick'>按钮1</button>
  <button data-id='2' bindtap='handleClick'>按钮2</button>
  <button data-id='3' bindtap='handleClick'>按钮3</button>
</view>

JS代码:

Page({
  handleClick: function(e) {
    const id = e.currentTarget.dataset.id;
    console.log('点击了按钮' + id);
  }
})

在点击事件中通过e.currentTarget.dataset.id来获取按钮的id参数。


原文地址: https://www.cveoy.top/t/topic/oh3x 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录