<view class="content"> <view class="hr"></view> <view class="bg"> <form bindsubmit="formSubmit" bindreset="formReset"> <view class="item"> <view class="name">联系人</view> <view class="value"> <input type="text" placeholder="收货人姓名" placeholder-class="holder" name="userName" value="{{userName}}" /> </view> </view> <view class="line"></view> <!-- <view class="item"> <view class="name">性别</view> <view class="value"> <radio-group class="radin-group" bindchange="radioChange" name="sex"> <radio value="0" checked="{{sex==0}}">先生</radio> <radio value="1" checked="{{sex==1}}">女士</radio> </radio-group> </view> </view> <view class="line"></view> --> <view class="item"> <view class="name">手机号码</view> <view class="value"> <input type="text" placeholder="联系您的电话" placeholder-class="holder" name="phone" value="{{phone}}" /> </view> </view> <view class="line"></view> <view class="item"> <view class="name">所在城市</view> <view class="value"> <picker mode="region" bindchange="bindRegionChange" value="{{region}}" custom-item="{{customItem}}" name="city"> <view class="picker"> {{region[0]}},{{region[1]}},{{region[2]}} </view> </picker> </view> </view> <!-- <view class="line"></view> <view class="item"> <view class="name">收货地址</view> <view class="value"> <input type="text" placeholder="选择收货地址" placeholder-class="holder" name="address" bindtap="chooseLocation" value="{{address}}" /> </view> </view> --> <view class="line"></view> <view class="item"> <view class="name">详细地址</view> <view class="value"> <input type="text" placeholder="请输入详细信息" placeholder-class="holder" name="fullAddress" value="{{fullAddress}}" /> </view> </view> <view class="line"></view> <button class="btn" form-type="submit">保存</button> <!-- <view class="tip">{{tip}}</view> --> </form> </view> </view>如何把输入的内容封装成一个类
时间: 2023-08-05 17:02:59 浏览: 94
这段代码是一个小程序中的 WXML 代码,用于展示一个表单页面。要把输入的内容封装成一个类,需要在小程序的后台代码中编写一个类(或者 JavaScript 对象),并定义相应的属性和方法来保存和处理表单数据。
具体来说,可以在一个 JavaScript 文件中定义一个类,例如:
```
class FormData {
constructor() {
this.userName = '';
this.phone = '';
this.region = [];
this.fullAddress = '';
}
setUserName(name) {
this.userName = name;
}
setPhone(phone) {
this.phone = phone;
}
setRegion(region) {
this.region = region;
}
setFullAddress(address) {
this.fullAddress = address;
}
getUserName() {
return this.userName;
}
getPhone() {
return this.phone;
}
getRegion() {
return this.region;
}
getFullAddress() {
return this.fullAddress;
}
}
```
在表单页面中,可以通过绑定事件,将用户输入的数据更新到该类的实例中:
```
Page({
data: {
formData: new FormData(),
},
formSubmit(e) {
const { userName, phone, region, fullAddress } = e.detail.value;
const formData = this.data.formData;
formData.setUserName(userName);
formData.setPhone(phone);
formData.setRegion(region);
formData.setFullAddress(fullAddress);
console.log(formData);
},
});
```
这样,就可以将用户输入的表单数据保存到一个类中,并在需要的时候进行处理和使用。
阅读全文