当前位置:网站首页>微信小程序中给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
})
}
})
相关链接
边栏推荐
- 在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
- Pourquoi Li shufu a - t - il construit son téléphone portable?
- Flink parsing (V): state and state backend
- Reppoints: advanced order of deformable convolution
- SAP UI5 框架的 manifest.json
- PyTorch 提取中间层特征?
- 酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
- Summary of Android interview questions of Dachang in 2022 (II) (including answers)
- 分布式(一致性协议)之领导人选举( DotNext.Net.Cluster 实现Raft 选举 )
- 李書福為何要親自掛帥造手機?
猜你喜欢

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

Reppoints: advanced order of deformable convolution

Smart street lamp based on stm32+ Huawei cloud IOT design

Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology

开源与安全的“冰与火之歌”

SQL statement optimization, order by desc speed optimization
![[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias](/img/03/ece7f7b28cd9caea4240635548c77f.jpg)
[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias

偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”

The NTFS format converter (convert.exe) is missing from the current system

Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
随机推荐
Pytest learning ----- detailed explanation of the request for interface automation test
Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
Alibaba brand data bank: introduction to the most complete data bank
Debug xv6
[getting started with MySQL] fourth, explore operators in MySQL with Kiko
面试突击62:group by 有哪些注意事项?
C # nanoframework lighting and key esp32
Vscode replaces commas, or specific characters with newlines
JMeter interface test response data garbled
【MySQL入门】第一话 · 初入“数据库”大陆
Example of batch update statement combining update and inner join in SQL Server
Shell input a string of numbers to determine whether it is a mobile phone number
C version selenium operation chrome full screen mode display (F11)
sql语句优化,order by desc速度优化
Smart street lamp based on stm32+ Huawei cloud IOT design
VR全景婚礼,帮助新人记录浪漫且美好的场景
Pyspark operator processing spatial data full parsing (4): let's talk about spatial operations first
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Flink parsing (IV): recovery mechanism
分布式不来点网关都说不过去