当前位置:网站首页>微信小程序中给event对象传递数据
微信小程序中给event对象传递数据
2022-07-06 09:53:00 【richest_qi】
文章目录
前情提要
给event对象传递id
- 给event对象传递id(
id="btn"):<button id="btn" bindtap="handleTap">点我一下</button> - 从event对象中获取id(
e.target.id):handleTap(e){ console.log(e.target.id); }
给event对象传递自定义属性
- 给event对象传递自定义属性(
data-type="confirm_btn"):<button data-type="confirm_btn" bindtap="handleTap">点我一下</button> - 从event对象中获取自定义属性(
e.target.dataset.type):handleTap(e){ console.log(e.target.dataset.type); }
小程序项目
向event对象传递id
代码涉及的主要文件有:
- app.json
- pages/index/index.wxss
- pages/index/index.wxml
- pages/index/index.js

app.json
{
"pages": [
"pages/index/index"
],
"window": {
"navigationBarBackgroundColor": "#0149af",
"navigationBarTitleText": "登录中心",
"navigationBarTextStyle": "white"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
pages/index/index.wxss
.container{
padding: 20rpx;
}
.container .title{
padding: 20rpx 0;
border-bottom: 1px solid #ddd;
font-weight: bold;
font-size: 32rpx;
}
.list{
margin: 40rpx auto 20rpx;
}
.item{
margin: 12rpx 0;
padding: 0 20rpx;
border: 1px solid #ddd;
border-radius: 6rpx;
}
.item input{
width: 100%;
height: 60rpx;
font-size: 28rpx;
}
button{
font-weight: normal;
font-size: 28rpx!important;
color: #fff;
background: #0149af;
}
pages/index/index.wxml
<view class="container">
<view class="title">登录</view>
<view class="list">
<view class="item">
<input type="text" id="username" bindinput="handleInput" placeholder="用户名" />
</view>
<view class="item">
<input type="text" password id="password" bindinput="handleInput" placeholder="密码" />
</view>
</view>
<button size="mini">登录</button>
</view>
pages/index/index.js
Page({
data:{
username:"",
password:""
},
handleInput(e){
const type = e.target.id;
this.setData({
[type]:e.detail.value
})
}
})
向event对象传递自定义属性
代码变更涉及到文件有:
- pages/index/index.wxml
- pages/index/index.js
pages/index/index.wxml
<view class="container">
<view class="title">登录</view>
<view class="list">
<view class="item">
<input type="text" data-type="username" bindinput="handleInput" placeholder="用户名" />
</view>
<view class="item">
<input type="text" password data-type="password" bindinput="handleInput" placeholder="密码" />
</view>
</view>
<button size="mini">登录</button>
</view>
pages/index/index.js
Page({
data:{
username:"",
password:""
},
handleInput(e){
const type = e.target.dataset.type;
this.setData({
[type]:e.detail.value
})
}
})
相关链接
边栏推荐
- 【MySQL入门】第四话 · 和kiko一起探索MySQL中的运算符
- Shell input a string of numbers to determine whether it is a mobile phone number
- Start job: operation returned an invalid status code 'badrequst' or 'forbidden‘
- Essai de pénétration du Code à distance - essai du module b
- Debug xv6
- Establishment of graphical monitoring grafana
- MySQL error reporting solution
- node の SQLite
- The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
- [introduction to MySQL] the first sentence · first time in the "database" Mainland
猜你喜欢

基本磁盘与动态磁盘 RAID磁盘冗余阵列区分

RepPoints:可形变卷积的进阶

Unity tips - draw aiming Center
![[ASM] introduction and use of bytecode operation classwriter class](/img/0b/87c9851e577df8dcf8198a272b81bd.png)
[ASM] introduction and use of bytecode operation classwriter class

Smart street lamp based on stm32+ Huawei cloud IOT design

FlutterWeb浏览器刷新后无法回退的解决方案

李书福为何要亲自挂帅造手机?

Zen integration nails, bugs, needs, etc. are reminded by nails

Optimization of middle alignment of loading style of device player in easycvr electronic map

李書福為何要親自掛帥造手機?
随机推荐
Pyspark operator processing spatial data full parsing (5): how to use spatial operation interface in pyspark
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
基于STM32+华为云IOT设计的智能路灯
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
It doesn't make sense without a distributed gateway
Kernel link script parsing
Xin'an Second Edition: Chapter 25 mobile application security requirements analysis and security protection engineering learning notes
【MySQL入门】第一话 · 初入“数据库”大陆
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
Solr appears write Lock, solrexception: could not get leader props in the log
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
Awk command exercise
Pytorch extract middle layer features?
Flink parsing (VII): time window
[getting started with MySQL] fourth, explore operators in MySQL with Kiko
The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
Getting started with pytest ----- test case rules
【MySQL入门】第三话 · MySQL中常见的数据类型
C# NanoFramework 点灯和按键 之 ESP32
Establishment of graphical monitoring grafana