微信小程序获取表单初始值,提交改变过的数据

 form表单,通过onload(options)获取的参数给输入框赋值,提交改变后的参数

<form bindsubmit="submitForm">
  <view class='item'>
    <view class='label'>联系人:</view>
    <input type="text" placeholder="" name="input_linker" value="{{linker}}" class="input_search" />
  </view>
  <view class='item'>
    <view class='label'>联系电话:</view>
    <input type="text" placeholder="" name="input_linkermobile" value="{{linkermobile}}" class="input_search" />
  </view>
  <view class='item'>
    <view class='label'>备注:</view>
    <textarea  placeholder="" name="input_remark" value="{{remark}}" class="textarea_search"></textarea>
  </view> 
  <view class='item'>
    <button class='bgBtn submitBtn' formType='submit' wx:if="{{isShow}}">提交</button>
  </view>
</form>
</view>
Page({
 data: {
    linker: '',
    linkermobile: '',
    remark: '',
    isShow: true,
    id: ''
  },
/*生命周期函数--监听页面加载*/
onLoad: function (options) {
    console.log(options);
    this.setData({
          id: res[0].id,
          linker: res[0].linker,
          linkermobile: res[0].linkermobile,
          remark: res[0].remark
        });
  },
/*点击提交 */
  submitForm: function(event){
    console.log(event.detail.value);
    var that = this;
    var obj1 = {};
    obj1.id = that.data.id; 
    if (that.data.linker != event.detail.value.input_linker){     
      obj1.linker = event.detail.value.input_linker;
    }
    if (that.data.linkermobile != event.detail.value.input_linkermobile) {
      obj1.linkermobile = event.detail.value.input_linkermobile;
    }
    if (that.data.remark != event.detail.value.input_remark) {
      obj1.remark = event.detail.value.input_remark;
    }
    wx.request({
      url: 'update.php', //仅为示例,并非真实的接口地址
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: obj1,
      success(res) {
        console.log(res.data);
        if(res.data.code == 1){
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            isShow: false
          });
        }
      }
    });
  },
})

猜你喜欢

转载自blog.csdn.net/LLL_liuhui/article/details/84333317
今日推荐