当前位置:网站首页>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;
}
边栏推荐
- Fastjson V2 简单使用手册
- pytorch 的Conv2d的详细解释
- 与AI结对编程式是什么体验 Copilot vs AlphaCode, Codex, GPT-3
- 电脑设备管理器在哪里可以找到
- Where can I find the computer device manager
- Redis的缓存穿透、缓存击穿和缓存雪崩
- 多线程经典案例
- What is flush software? In addition, is it safe to open an account online now?
- Analysis of PostgreSQL storage structure
- B_ QuRT_ User_ Guide(33)
猜你喜欢

latex左侧大括号 latex中大括号多行公式

WinDbg debugging tool introduction

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

Mysql:sql overview and database system introduction | dark horse programmer

How does win11 optimize services? Win11 method of optimizing service

Redis的缓存穿透、缓存击穿和缓存雪崩

Some memory problems summarized

The sandbox is being deployed on the polygon network

总结的一些内存问题

Classic case of multithreading
随机推荐
在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言
Interesting plug-ins summary
深入解析 Apache BookKeeper 系列:第四篇—背压
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
When unittest automatically tests multiple use cases, the logging module prints repeatedly to solve the problem
A new one from Ali 25K came to the Department, which showed me what the ceiling is
在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
Redis的事务和锁机制
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
Apache server OpenSSL upgrade
2022中国国潮发展新动向
基于kubernetes平台微服务的部署
项目管理到底管的是什么?
pytorch 的Conv2d的详细解释
Femas: cloud native multi runtime microservice framework
Win11电脑名如何更改?Win11更改电脑名的方法
从PG15 XID64再次跳票说起
Nansen double disk encryption giant self rescue: how to prevent the collapse of billions of dominoes
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
深入解析 Apache BookKeeper 系列:第四篇—背压