当前位置:网站首页>微信小程序中给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
})
}
})
相关链接
边栏推荐
- Interview assault 63: how to remove duplication in MySQL?
- Start job: operation returned an invalid status code 'badrequst' or 'forbidden‘
- SQL statement optimization, order by desc speed optimization
- Pytorch extract middle layer features?
- Error: Publish of Process project to Orchestrator failed. The operation has timed out.
- kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
- Quick start of Hongmeng system
- 【ASM】字节码操作 ClassWriter 类介绍与使用
- Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
- Reppoints: advanced order of deformable convolution
猜你喜欢

C WinForm series button easy to use

Interview shock 62: what are the precautions for group by?

Unity tips - draw aiming Center

C version selenium operation chrome full screen mode display (F11)

Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability

EasyCVR电子地图中设备播放器loading样式的居中对齐优化

The solution that flutterweb browser cannot be rolled back after refreshing

Getting started with pytest ----- test case pre post, firmware

PySpark算子处理空间数据全解析(5): 如何在PySpark里面使用空间运算接口

中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
随机推荐
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
Automatic operation and maintenance sharp weapon ansible Foundation
It doesn't make sense without a distributed gateway
JMeter interface test response data garbled
基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
Pytest learning ----- detailed explanation of the request for interface automation test
The art of Engineering (3): do not rely on each other between functions of code robustness
Distinguish between basic disk and dynamic disk RAID disk redundant array
Establishment of graphical monitoring grafana
In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
Single responsibility principle
酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
2022年大厂Android面试题汇总(一)(含答案)
Basic configuration and use of spark
Manifest of SAP ui5 framework json
10 advanced concepts that must be understood in learning SQL
RepPoints:可形变卷积的进阶
Zen integration nails, bugs, needs, etc. are reminded by nails