Parameterize button bind function for wechat miniprogram

50 Views Asked by At

I'm trying with wechat miniprogram for the first time and believe its still web pages but with its own tag (and handling).

I'm trying to do something like this:

  <view class="btns">
    <button bindtap="getWeather('New York')">New York</button>
    <button bindtap="getWeather('Toronto')">Toronto</button>
  </view>

IE for the web page buttons, I want to call the getWeather function, but different buttons will pass different parameters to the function.

How can I do it in wechat miniprogram and also html/javascript in general as well please?

UPDATE:

My code in theory should work with html/javascript in general, like:

So this question only limits to wechat miniprogram then.

1

There are 1 best solutions below

0
On BEST ANSWER

"bindtap="getWeather('New York')" ,the parameterize will not transform in normal.Wechat miniprogram use the way below:

Page({
  data: {
    selectedRole: null
  },
  eventFunc(e) {
    console.log(e)
    this.setData({
      selectedRole: e.target.dataset.role
    })
  }
})
<Button bindtap="eventFunc" data-role="teacher">teacher</Button>
<Button bindtap="eventFunc" data-role="student">student</Button>
selectedRole:{{selectedRole}}

Now, developing with Wechat Native language decreased a lot.More developers use other frame (use vue or react) to transform into Wechat Miniprogram Native Codes and run.