当前位置:网站首页>微信小程序中的数据双向绑定
微信小程序中的数据双向绑定
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;
}
边栏推荐
- Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning
- 部门新来了个阿里25K出来的,让我见识到了什么是天花板
- leetcode:104. 二叉树的最大深度
- Pytorch quantitative practice (2)
- Notes [introduction to JUC package and future]
- Stinky tofu made by Grandma
- Meet the StreamNative | 杨子棵:是什么让我放弃了大厂 Offer
- Niubi | the tools I have treasured for many years have made me free to fish with pay
- B_ QuRT_ User_ Guide(35)
- [untitled] first time to participate in CSDN activities
猜你喜欢
《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26
Analysis of PostgreSQL storage structure
[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)
电脑版微信文件存储在哪个文件夹可以找到
Some memory problems summarized
[micro service ~nacos] configuration center of Nacos
Study summary of dynamic routing between capsules
Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning
How to realize the center progress bar in wechat applet
谈谈数字化转型的几个关键问题
随机推荐
电脑版微信文件存储在哪个文件夹可以找到
KVM IO性能测试数据
Error reporting: internal error XFS_ WANT_ CORRUPTED_ GOTO at line 1635 of file fs/xfs/libxfs/xfs_ alloc. c.
【MySQL入门】第一话 · 初入“数据库”大陆
Is the stock account opened in qiniu safe? How to apply for a low commission stock account?
Go language learning notes - Gorm usage - database configuration, table addition | web framework gin (VII)
十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
将Nagios监控信息存入MySQL
【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)
Summary of interesting websites
Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning
Uniapp third party network request
Document layout analysis: a comprehensive survey 2019 paper learning summary
总结的一些内存问题
How to use data sets in machine learning?
Win11电脑名如何更改?Win11更改电脑名的方法
Online education program user login and registration
The Three Musketeers: One for All!
谈谈数字化转型的几个关键问题