1. 程式人生 > 程式設計 >微信小程式 bindtap 傳參的例項程式碼

微信小程式 bindtap 傳參的例項程式碼

微信小程式 bindtap 傳參 ,程式碼如下所示:

//index.wxml
<view bindtap="changeIndex" data-src="我固定引數">
</view>
//index.js
page({
 data:{
 },changeIndex(e){
 console.log(e.currentTarget.dataset.src); //我是固定引數
 }
});

可以看出 引數是通過給標籤設定 data-引數名=“引數值” 自定義屬性的方式 來傳遞的 例如想傳遞兩個引數

//index.wxml
<view bindtap="changeIndex" data-src1="我固定引數1" data-src2="我是固定引數2" >
 
</view>
//index.js
page({
 data:{
 
 },changeIndex(e){
 console.log(e.currentTarget.dataset.src1); //我是固定引數1
 console.log(e.currentTarget.dataset.src2); //我是固定引數2
 }
});

如果需要傳遞動態的引數 例如遍歷渲染時 想傳遞 index 給 changeIndex方法

//index.wxml
<view wx:for="{{lists}}" wx:for-index="index" wx:key="index" data-index="{{index}}" >
{{item.title}}
</view>
//index.js
page({
 data:{
 lists:[{title:'引數1',id:1},{title:'引數2',id:2}]
 },changeIndex(e){
 console.log(e.currentTarget.dataset.index);
 }
})

知識點補充:

微信小程式:bindtap方法傳參

1、wxml

<view bindtap="pay_again" data-name="{{orderList.jid}}" data-fee="{{orderList.act_fee}}" data-mobile="{{orderList.p_phone}}" data-act="{{orderList.act_name}}" class="operating f_r webkit-box" style="line-height:30px;"> 
   <a href="" class=" rel="external nofollow" pay bg_red">繼續支付</a> 
  </view>

2、js

// 再次發起支付請求,呼叫後臺PHP
 pay_again:function(e){
 var that = this;
 that.setData({
  jid: e.currentTarget.dataset.name,act_name: e.currentTarget.dataset.act,act_fee: e.currentTarget.dataset.fee,mobile: e.currentTarget.dataset.mobile
 })
 console.log('活動訂單id = ' + that.data.mobile);
 }

總結

到此這篇關於微信小程式 bindtap 傳參的例項程式碼的文章就介紹到這了,更多相關微信小程式 bindtap 傳參內容請搜素我們以前的文章或下面相關文章,希望大家以後多多支援我們!