【小程序】清空input框内容

WXML:

<form bindreset="foo">
  <input bindinput="bindKeyInput" placeholder="在此输入"/>
  <button form-type="reset">发送</button>
</form>

js:

Page({
  data: {
    inputValue:''//绑定的输入框文本
  },
  bindKeyInput: function(e) {
    this.setData({
      inputValue: e.detail.value//将input至与data中的inputValue绑定
    })
  },
  foo:function(){
    if(this.data.inputValue){
      //Do Something
    }else{
      //Catch Error
    }
    this.setData({
        inputValue:''//将data的inputValue清空
    });
    return;
  }
})

总结&注意:

  1. 类似于组件传递事件的思想
  2. 将input与button装在一个form中,button触发reset,form通过bindreset接到reset事件后触发foo方法
  3. form-type=”reset” 一定要写在button标签内(写在view中无效,本人踩过坑)
  4. 即便input框内容通过reset清空了,但我们还是需要在方法中将input绑定的值重置

以上。不正指出请前辈们指教!

猜你喜欢

转载自blog.csdn.net/Maximus_ckp/article/details/71172530
今日推荐