微信小程序开发笔记 基础篇③——自定义数据dataset,事件触发携带额外信息

该文章介绍了如何使用微信小程序开发一个电费充值界面,通过在不同金额的充值按钮上设置dataset属性,传递自定义数据(如金额)。当用户点击按钮时,事件回调函数能获取到点击的按钮所携带的数据,从而实现动态更新界面并展示所选金额。示例代码包括了页面结构、事件处理和数据绑定的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前言

微信小程序开发笔记——导读

  • 想要实现一个电费充值界面。
  • 多个不同金额的充值按钮,每个按钮都携带自定义数据(金额)
  • 点击不同金额的充值按钮,就会上传对应的数据(金额)。
  • 所以,本文主要使用到了微信小程序框架的视图层的事件系统的事件对象的dataset

二、视频演示

微信小程序 充值界面简单演示

三、原理和流程

  • 在对应的组件控件上面添加dataset属性
<view bindtap="rechargePriceClick" data-totalfee="10">
    <text style="font-size: 40rpx;">10</text>
    <text style="font-size: 26rpx;"></text>
</view>
  • 那么它的点击事件回调函数里面就会回传这个属性
  rechargePriceClick(e){
      console.log(e)
  },
  • 日志打印与验证
    在这里插入图片描述

四、注意事项

在这里插入图片描述

  • 验证
<view bindtap="rechargePriceClick" data-aUser="10" data-b-user="10">
    <text style="font-size: 40rpx;">10</text>
    <text style="font-size: 26rpx;"></text>
</view>

在这里插入图片描述

五、全部源码

  • recharge.js
Page({
  data: {
    totalFee: 0,
  },

  onLoad: function (e) {
    console.log("recharge onLoad")
  },

  rechargePriceClick(e){
      var _this = this
      console.log(e.currentTarget.dataset)
      this.setData({
        totalfee: e.currentTarget.dataset.totalfee
      })
      
    wx.showToast({
        title: "totalfee="+_this.data.totalfee,
        icon: 'none',
        duration: 1000
    })
  },

  rechargeClick(e) {
    var _this = this
    console.log("rechargeClick", this.data)  
    wx.showToast({
        title: "totalfee="+_this.data.totalfee,
        icon: 'none',
        duration: 1000
    }) 
  },
})
  • recharge.json
{
  "navigationBarTitleText": "电费充值",
  "usingComponents": {}
}
  • recharge.wxml
<view class="container">
    <view style="margin-top: 20rpx; margin-left: 50rpx; margin-right: 50rpx; align-items: center; display: flex; align-items: center; font-size: 40rpx;">
        <view class="button_frame {{totalfee=='10'?'button_select':'button_unselect'}}" style="margin-right: 30rpx;" bindtap="rechargePriceClick" data-totalfee="10">
            <text style="font-size: 40rpx;">10</text>
            <text style="font-size: 26rpx;"></text>
        </view>
        <view class="button_frame {{totalfee=='20'?'button_select':'button_unselect'}}" style="margin-right: 30rpx;" bindtap="rechargePriceClick" data-totalfee="20">
            <text style="font-size: 40rpx;">20</text>
            <text style="font-size: 26rpx;"></text>
        </view>
        <view class="button_frame {{totalfee=='50'?'button_select':'button_unselect'}}" bindtap="rechargePriceClick" data-totalfee="50">
            <text style="font-size: 40rpx;">50</text>
            <text style="font-size: 26rpx;"></text>
        </view>
    </view>
    <view style="margin-top: 20rpx; margin-left: 50rpx; margin-right: 50rpx; align-items: center; display: flex; align-items: center; font-size: 40rpx;">
        <view class="button_frame {{totalfee=='100'?'button_select':'button_unselect'}}" style="margin-right: 30rpx;" bindtap="rechargePriceClick" data-totalfee="100">
            <text style="font-size: 40rpx;">100</text>
            <text style="font-size: 26rpx;"></text>
        </view>
        <view class="button_frame {{totalfee=='200'?'button_select':'button_unselect'}}" style="margin-right: 30rpx;" bindtap="rechargePriceClick" data-totalfee="200">
            <text style="font-size: 40rpx;">200</text>
            <text style="font-size: 26rpx;"></text>
        </view>
        <view class="button_frame {{totalfee=='500'?'button_select':'button_unselect'}}" bindtap="rechargePriceClick" data-totalfee="500">
            <text style="font-size: 40rpx;">500</text>
            <text style="font-size: 26rpx;"></text>
        </view>
    </view>
    <view class="btnBox btnBox_electricBg" bindtap="rechargeClick">立即充值</view>
</view>
  • recharge.wxss
.button_frame {
    width: 200rpx; 
    padding-top: 40rpx; 
    padding-bottom: 40rpx; 
    padding-right: 40rpx; 
    padding-left: 40rpx; 
    border-radius: 10rpx; 
    text-align: center;
}

.button_select {
    color: #ffffff;
    background-color: #58d4dc;
}

.button_unselect {
    color: black; 
    background-color: gainsboro;
}

六、参考

觉得好,就一键三连呗(点赞+收藏+关注)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小康师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值