当前位置:网站首页>微信小程序中给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
})
}
})
相关链接
边栏推荐
- Reppoints: advanced order of deformable convolution
- FlutterWeb瀏覽器刷新後無法回退的解决方案
- 一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
- Compile and build, from the bottom to the top
- Run xv6 system
- Concept and basic knowledge of network layering
- Hongmeng introduction and development environment construction
- Growth of operation and maintenance Xiaobai - week 7
- BearPi-HM_ Nano development board "flower protector" case
- EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
猜你喜欢

Uipath browser performs actions in the new tab

There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house

SQL statement optimization, order by desc speed optimization

在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?

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

How to use scroll bars to dynamically adjust parameters in opencv

sql语句优化,order by desc速度优化
![[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

78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO

Automatic operation and maintenance sharp weapon ansible Foundation
随机推荐
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
Flink parsing (VII): time window
The shell generates JSON arrays and inserts them into the database
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
Flink parsing (V): state and state backend
Unity particle special effects series - treasure chest of shining stars
传统家装有落差,VR全景家装让你体验新房落成效果
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
EasyCVR授权到期页面无法登录,该如何解决?
[rapid environment construction] openharmony 10 minute tutorial (cub pie)
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
RepPoints:可形变卷积的进阶
Getting started with pytest ----- test case rules
Kali2021 installation and basic configuration
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
远程代码执行渗透测试——B模块测试
[translation] principle analysis of X Window Manager (I)
Xin'an Second Edition: Chapter 24 industrial control safety demand analysis and safety protection engineering learning notes
【MySQL入门】第一话 · 初入“数据库”大陆
The solution to the left-right sliding conflict caused by nesting Baidu MapView in the fragment of viewpager