WeChat applet submission form clears the input box

Here are two simple ways to record:
First, bind the object value in the input, and reset the value to null in the callback after successful submission. Personally feel that this method is suitable for less form data.
wxml code

<form bindsubmit="submitForm">
    <input name="title"  maxlength="20" value="{{title}}" />
    <input name="txt"  value="{{txt}}" />
    <button  formType="submit">提交</button>
<form>

js code

submitForm: function(e) {
  var that = this;
  var formData = e.detail.value;
  wx.request({
    url: apiurl,  //你要提交的接口
    data: formData,
    header: { 'Content-Type': 'application/json' },
    success: function (res) {
      if (res.data.status == 200) {
        wx.showToast({
          title: '提交成功',
        })
        that.setData({
          title: '',
          txt: ''
        })
      }
    }
  })
}

Second, reset the event through the form reset, but this method cannot directly use the form form to submit data to the e.detail.value in the submit method provided by us. All we need to use the bindinput or bindblur event of the input to get the form data, save the data setData. Then you can use the formReset method to pass the saved data to the background. But one thing to note before submitting is to check whether the data is empty and the data format meets the regulations.

<form bindreset="formReset">
  <input name="title" data-name="title" maxlength="20" bindblur="getData" />
  <input name="txt" data-name="txt" bindblur="getData" />
  <button  formType="reset">提交</button>
</form>

js code

data:{
    cont:{
      title: "",
      txt: ""
    }
  },
  getData:function(e){
    //console.log(e)
    var name = e.currentTarget.dataset.name;
    this.data.cont[name] = e.detail.value;
    this.setData({
      cont: this.data.cont
    })
    console.log(this.data.cont)
  },
  formReset:function(e){
    //在此将保存的表单数据传递给后台
  }
Published 21 original articles · won praise 1 · views 7809

Guess you like

Origin blog.csdn.net/eva_feng/article/details/105119955