【小程序】清空input框内容

xiaoxiao2021-02-27  372

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; } })

总结&注意:

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

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

转载请注明原文地址: https://www.6miu.com/read-5565.html

最新回复(0)