微信小程序--获取form表单初始值提交数据

时间:2021-09-27 19:48:20
<form bindsubmit="formSubmit">
<view class="txt">
<view class="ima"></view>
<view class="txt2">姓名</view>
<input placeholder="请输入姓名" maxlength="10" class="txt3" value="{{mem_name}}" bindchange="name" name="name2"/>
</view>
<view class="txt">
<view class="ima"></view>
<view class="txt2">身份证号</view>
<input type="idcard" placeholder="请输入身份证号码" class="txt3" value="{{mem_IDnum}}" bindchange="ID_num" maxlength="18" name="ID_num2"/>
</view>
<view style="clear:both"></view>
<button class="btn" form-type="submit" hover-class="none">完成</button>
</form>
page({
name: function (e) { //获取input输入的值
var that = this;
that.setData({
name: e.detail.value
})
},
ID_num: function (e) { //获取input输入的值
var that = this;
that.setData({
ID_num: e.detail.value
})
var id_num = that.data.ID_num
if (!(id_num.length === 15 || id_num.length === 18)) {
wx.showToast({
title: '请输入15或18位数身份证号码',
image: '../Image/error.png',
duration: 2000
})
}
}, formSubmit: function (e) {
var that = this;
var tokend = wx.getStorageSync('tokend')
var name2 = e.detail.value.name2; //获取input初始值
var ID_num2 = e.detail.value.ID_num2; //获取input初始值
var name = that.data.name ? that.data.name : name2 //三元运算,如果用户没修改信息,直接提交原来的信息,如果用户修改了信息,就将修改了的信息和未修改过的信息一起提交
var ID_num = that.data.ID_num ? that.data.ID_num : ID_num2
wx.request({
method: 'POST',
url: 'https://....?token=' + tokend, //接口地址
data: {
'name': name,
'ID_num': ID_num
},
header: { 'content-type': 'application/json' },
success: function (res) {
wx.showToast({
title: '资料修改成功',
image: '../Image/suess.png',
duration: 2000
})
setTimeout(function () {
wx.switchTab({
url: '../index/index',
})
}, 2000) },
fail: function (res) {
console.log('cuowu' + ':' + res)
}
})
},
})