当前位置:网站首页>微信小程序中的数据双向绑定
微信小程序中的数据双向绑定
2022-06-30 22:15:00 【水星记_】
前言
微信小程序因为诸多限制所以它无法像 vue 那样通过指令进行数据绑定,那微信小程序有没有什么办法可以实现数据的双向绑定呢?今天给大家分享两种微信小程序数据绑定的方法。
通过 model:value 绑定
当点击 form 表单中 form-type 为 submit 的 button 组件时,会将表单组件中的 value 值进行提交,需要在表单组件中加上 name 来作为 key。
.wxml 文件
<form catchsubmit="formSubmit">
<view class="formItemBox">
<view>姓名</view>
<view>
<input name="name" model:value="{
{ formData.name }}" type="text" placeholder="请输入姓名" />
</view>
</view>
<view class="formItemBox">
<view>年龄</view>
<view>
<input name="age" model:value="{
{ formData.age }}" type="text" placeholder="请输入年龄" />
</view>
</view>
<view class="formItemBox">
<view>号码</view>
<view>
<input name="number" model:value="{
{ formData.number }}" type="text" placeholder="请输入号码" />
</view>
</view>
<view class="formItemBox">
<view>地址</view>
<view>
<input name="address" model:value="{
{ formData.address }}" type="text" placeholder="请输入地址" />
</view>
</view>
<!-- 底部操作按钮 -->
<view>
<button formType="submit">提交</button>
</view>
</form>
.js 文件
Page({
data: {
// 绑定的数据
formData: {
name: "",
age: "",
number: "",
address: "",
},
},
// 提交操作
formSubmit(e) {
console.log(e.detail.value); //提交的数据
},
})
点击提交操作后:
通过 bindinput 方法
首先用标签属性 data- 绑定 js 中的 data 参数,再通过 bandinput 事件获取 key 值,最后利用模板字符串赋值给 data 中的值。
.wxml 文件
<view>
<view class="formItemBox">
<view>姓名</view>
<view>
<input data-gater="formData.name" bindinput="inputFrame" value="{
{ formData.name }}" type="text"
placeholder="请输入姓名" />
</view>
</view>
<view class="formItemBox">
<view>年龄</view>
<view>
<input data-gater="formData.age" bindinput="inputFrame" value="{
{ formData.age }}" type="text"
placeholder="请输入年龄" />
</view>
</view>
<view class="formItemBox">
<view>号码</view>
<view>
<input data-gater="formData.number" bindinput="inputFrame" value="{
{ formData.number }}" type="text"
placeholder="请输入号码" />
</view>
</view>
<view class="formItemBox">
<view>地址</view>
<view>
<input data-gater="formData.address" bindinput="inputFrame" value="{
{ formData.address }}" type="text"
placeholder="请输入地址" />
</view>
</view>
<!-- 底部操作按钮 -->
<view>
<button bindtap="submit">提交</button>
</view>
</view>
.js 文件
Page({
data: {
// 绑定的数据
formData: {
name: "",
age: "",
number: "",
address: "",
},
},
// 提交操作
submit() {
console.log(this.data.formData); //提交的数据
},
// input事件(内容改变时触发)
inputFrame(e) {
this.setData({
[`${
e.currentTarget.dataset.gater}`]: e.detail.value
})
console.log(this.data.formData);
},
})
input框输入和提交时:
最后附上
.wxss文件
.formItemBox {
display: flex;
margin: 0rpx 10rpx;
padding: 14rpx 0rpx;
border-bottom: 1px solid gainsboro;
}
.formItemBox view:first-child {
width: 20%;
color: #323333;
font-size: 28rpx;
}
.formItemBox view:last-child {
width: 80%;
}
input {
font-size: 24rpx;
color: #626263;
}
边栏推荐
- latex字母头顶两个点
- 公有云市场迈入深水区,冷静的亚马逊云还坐得住吗?
- Nansen复盘加密巨头自救:如何阻止百亿多米诺倾塌
- Store Nagios monitoring information into MySQL
- Why does the computer speed slow down after vscode is used for a long time?
- 「团队训练赛」ShanDong Multi-University Training #3
- win11更新后任务栏空白怎么办? win11更新后任务栏空白卡死的解决方法
- 牛逼|珍藏多年的工具让我实现了带薪摸鱼自由
- Which direction should college students choose to find jobs after graduation?
- 《安富莱嵌入式周报》第270期:2022.06.13--2022.06.19
猜你喜欢

顺祝老吴的聚会

电脑版微信文件存储在哪个文件夹可以找到

Uniapp life cycle / route jump

Web APIs comprehensive case -tab column switching - dark horse programmer

B_ QuRT_ User_ Guide(31)

Domestic database disorder
![[BSP video tutorial] BSP video tutorial issue 19: AES encryption practice of single chip bootloader, including all open source codes of upper and lower computers (June 26, 2022)](/img/ce/9ec74c4c26513e2479df29a1802168.png)
[BSP video tutorial] BSP video tutorial issue 19: AES encryption practice of single chip bootloader, including all open source codes of upper and lower computers (June 26, 2022)
Notes [introduction to JUC package and future]

How to use data sets in machine learning?

2022中国国潮发展新动向
随机推荐
Notes [introduction to JUC package and future]
Uniapp routing uni simple router
B_ QuRT_ User_ Guide(35)
AtCoder Beginner Contest 257
On several key issues of digital transformation
Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning
Apache server OpenSSL upgrade
1. Summary of wechat applet page Jump methods; 2. the navigateto stack does not jump to the tenth floor
Is there a shortage? No need to download the free online resources! 2022 favorites must have it!
How to upload binary pictures in uniapp
请指教同花顺软件究竟是什么?另外想问,现在在线开户安全么?
10 airbags are equipped as standard, and Chery arizer 8 has no dead corner for safety protection
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan, and Samsung sk of South Korea scrambled for a raise to retain semiconductor talents; Fir
B_ QuRT_ User_ Guide(34)
100 important knowledge points that SQL must master: creating and manipulating tables
Mysql:sql overview and database system introduction | dark horse programmer
"Trust machine" empowers development
B_ QuRT_ User_ Guide(33)
Analysis of PostgreSQL storage structure
MFC interface library bcgcontrolbar v33.0 - desktop alarm window, grid control upgrade, etc