当前位置:网站首页>Two way data binding in wechat applet
Two way data binding in wechat applet
2022-06-30 22:33:00 【Mercury_】
Preface
Due to many restrictions, wechat applets cannot be like vue Data binding through instructions , Is there any way for wechat applets to achieve two-way data binding ? Today, I will share with you two methods of data binding for wechat applets .
adopt model:value binding
When you click form In the form form-type by submit Of button When the component , In the form component value Worth submitting , You need to add... To the form component name As a key.
.wxml file
<form catchsubmit="formSubmit">
<view class="formItemBox">
<view> full name </view>
<view>
<input name="name" model:value="{
{ formData.name }}" type="text" placeholder=" Please enter a name " />
</view>
</view>
<view class="formItemBox">
<view> Age </view>
<view>
<input name="age" model:value="{
{ formData.age }}" type="text" placeholder=" Please enter age " />
</view>
</view>
<view class="formItemBox">
<view> number </view>
<view>
<input name="number" model:value="{
{ formData.number }}" type="text" placeholder=" Please enter the number " />
</view>
</view>
<view class="formItemBox">
<view> Address </view>
<view>
<input name="address" model:value="{
{ formData.address }}" type="text" placeholder=" Please enter the address " />
</view>
</view>
<!-- Bottom operation button -->
<view>
<button formType="submit"> Submit </button>
</view>
</form>
.js file
Page({
data: {
// Bound data
formData: {
name: "",
age: "",
number: "",
address: "",
},
},
// Submit operation
formSubmit(e) {
console.log(e.detail.value); // Data submitted
},
})
Click Submit :
adopt bindinput Method
First, use the tag attribute data- binding js Medium data Parameters , Re pass bandinput Event acquisition key value , Finally, the template string is used to assign values to data The value in .
.wxml file
<view>
<view class="formItemBox">
<view> full name </view>
<view>
<input data-gater="formData.name" bindinput="inputFrame" value="{
{ formData.name }}" type="text"
placeholder=" Please enter a name " />
</view>
</view>
<view class="formItemBox">
<view> Age </view>
<view>
<input data-gater="formData.age" bindinput="inputFrame" value="{
{ formData.age }}" type="text"
placeholder=" Please enter age " />
</view>
</view>
<view class="formItemBox">
<view> number </view>
<view>
<input data-gater="formData.number" bindinput="inputFrame" value="{
{ formData.number }}" type="text"
placeholder=" Please enter the number " />
</view>
</view>
<view class="formItemBox">
<view> Address </view>
<view>
<input data-gater="formData.address" bindinput="inputFrame" value="{
{ formData.address }}" type="text"
placeholder=" Please enter the address " />
</view>
</view>
<!-- Bottom operation button -->
<view>
<button bindtap="submit"> Submit </button>
</view>
</view>
.js file
Page({
data: {
// Bound data
formData: {
name: "",
age: "",
number: "",
address: "",
},
},
// Submit operation
submit() {
console.log(this.data.formData); // Data submitted
},
// input event ( Trigger when content changes )
inputFrame(e) {
this.setData({
[`${
e.currentTarget.dataset.gater}`]: e.detail.value
})
console.log(this.data.formData);
},
})
input Box input and submit :
The last attached
.wxssfile
.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;
}
边栏推荐
- 【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(图像分类篇)
- 与AI结对编程式是什么体验 Copilot vs AlphaCode, Codex, GPT-3
- Femas:云原生多运行时微服务框架
- Ten of the most heart piercing tests / programmer jokes, read the vast crowd, how to find?
- 2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main() { ints := make
- [micro service ~nacos] configuration center of Nacos
- 深入解析 Apache BookKeeper 系列:第四篇—背压
- Win11如何优化服务?Win11优化服务的方法
- 深入解析 Apache BookKeeper 系列:第四篇—背压
- leetcode:104. 二叉树的最大深度
猜你喜欢

JVM Part 21 of interview with big companies Q & A

Nansen double disk encryption giant self rescue: how to prevent the collapse of billions of dominoes

远程办公期间,项目小组微信群打卡 | 社区征文

【BSP视频教程】BSP视频教程第19期:单片机BootLoader的AES加密实战,含上位机和下位机代码全开源(2022-06-26)

零样本和少样本学习

深入解析 Apache BookKeeper 系列:第四篇—背压

Anfulai embedded weekly report no. 270: June 13, 2022 to June 19, 2022

理想中的接口自动化项目

在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言

Deployment of microservices based on kubernetes platform
随机推荐
Is the stock account opened in qiniu safe? How to apply for a low commission stock account?
100 important knowledge points that SQL must master: creating and manipulating tables
电脑版微信文件存储在哪个文件夹可以找到
Starting from pg15 xid64 ticket skipping again
Two dots on the top of the latex letter
Niubi | the tools I have treasured for many years have made me free to fish with pay
The Three Musketeers: One for All!
Spark - understand partitioner in one article
Go language learning notes - Gorm usage - database configuration, table addition | web framework gin (VII)
Apache服务器OpenSSL升级
《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26
请指教同花顺软件究竟是什么?另外想问,现在在线开户安全么?
How does win11 optimize services? Win11 method of optimizing service
Online education program user login and registration
The Sandbox 正在 Polygon 网络上进行部署
latex中 & 号什么含义?
win11更新后任务栏空白怎么办? win11更新后任务栏空白卡死的解决方法
与AI结对编程式是什么体验 Copilot vs AlphaCode, Codex, GPT-3
图纸加密如何保障我们的核心图纸安全
Analysis of PostgreSQL storage structure