1. 程式人生 > >微信小程式 清空input框內容

微信小程式 清空input框內容

需求為:點擊發送之後,傳送內容,並將傳送的內容清空

不做任何處理的話,小程式是不會幫你清空表單的;本次程式碼如下:

wxml

<form bindreset="foo">
    <input bindinput='getMsg' name="msg">請輸入...</input>
    <button form-type="reset">傳送</button>
</form>

js

  foo: function () {
    if (this.data.content) {
      //Do Something
      this.saveGMAMessage()
    } else {
      //Catch Error
     
    }
    this.setData({
      content: ''//將data的inputValue清空
    });
    return;
  },
  getMsg(e){
    this.setData({
      content:e.detail.value
    })
  },

原理就是點選提交,觸發表單reset事件,判斷是否有內容輸入,有則執行傳送請求;然後將已經繫結content的input值清空;