当前位置:网站首页>【微信小程序2】事件传参与数据同步[03]
【微信小程序2】事件传参与数据同步[03]
2022-08-03 19:45:00 【m0_65431212】
为data中的数据赋值
通过调用this.setData(dataObject)方法,可以给页面data中 数据重新赋值
//页面的.js文件中
Page({
data:{
count: 0
},
changeCount(){
this.setData({
count: this.data.count+1
})
}
})
配套
//在.wxml中
<button type="primary" bindtap="CountChange">+1</button>
事件传参
- 错误示例
<button type="primary" bindtap='btn(123)'>事件传参</button>
此时,小程序会把(123)当成是一个统一的时间名称来处理
- 正确示范
<button bindtap="btn" data-info="{
{2}}">事件传参</button>
最终:
- info会被解析为参数的名字
- 数值2会被解析为参数的值
- 不加{ {}}的2被解析为文本形式
- 使用event.target.dataset.参数名 即可获得到具体参数的值
配套
//在.js文件中
btn(e){
console.log(e)
},
点击按钮后:
bindinput的语法格式-获取文本输入框内值
- 通过bindinput,可以为文本框绑定输入事件
//.wxml文件中
<input bindinput="inputHandler"><input>
- 在页面.js文件中定义事件处理函数
inputHandler(e){
//e.detail.value是变化过后,文本框最新的值
console.log(e.detail.value)
}
实现文本框和data之间的数据同步
- 在.js文件中定义data
data{
msg: '你好,'
},
- 在上一步的基础上,改造.wxml文件
<input value="{
{msg}}" bindinput="inputHandler"></input>
此时,页面文本框效果如下:
- 通过修改.wxss进一步美化
input{
border: 1px solid #eee;
margin: 5px;
padding: 5px;
border-radius: 3px;
}
此时,文本框效果:
4. 获取文本框内最新值
inputHandler(e){
//e.detail.value是变化过后,文本框最新的值
// console.log(e.detail.value)
this.setData{
msg: e.detail.value
}
边栏推荐
- C中的数据存储
- docker mysql 容器中执行mysql脚本文件并解决乱码
- 入门3D建模基础教程详细分解
- 数据驱动的软件智能化开发| ChinaOSC
- PHP according to the longitude and latitude calculated distance two points
- 「学习笔记」高斯消元
- Calculation of the array serial number of Likou brush questions (one question per day 7/28)
- 边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型
- 「游戏建模干货」建模大师几步操作,学习经典,赶紧脑补一下吧
- matplotlib画polygon, circle
猜你喜欢
随机推荐
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
Statistical machine learning 】 【 linear regression model
Standard C language learning summary 11
matplotlib画polygon, circle
【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃
建模该从哪一步开始?给你分析,给零基础的你一些学习建议
那些年我写过的语言
The effective square of the test (one question of the day 7/29)
单调栈及其应用
MySQL master-slave, 6 minutes you master!
FreeRTOS中级篇
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
Detailed AST abstract syntax tree
LeetCode 952. 按公因数计算最大组件大小
Reveal how the five operational management level of hundreds of millions of easily flow system
ERROR: You don‘t have the SNMP perl module installed.
机器学习中专业术语的个人理解与总结(纯小白)
Solution for no navigation bar after Word is saved as PDF