废话不多说,调了3个小时,终于有答案了
<input @input="inputCheck" v-model="amount"/>
脚本则是
methods: { inputCheck(el) { var that = this; var idx = -1; var value = el.detail.value.replace(/[^0-9.]/g,'').trim(); if(value == "") { setTimeout(function(){ that.amount = ''; },100); return; } idx = value.indexOf('.'); var raw = value; if(idx > 0) { var ext = value.slice(idx+1,idx+3) if (ext.indexOf('.') > -1 ) { ext = ext.slice(0,ext.indexOf('.')); } if(ext.length > 2 ) { ext = ext.slice(0,2); } value = value.slice(0,idx) + '.' + ext; } else if(idx == 0) { value = '0.' } setTimeout(function(){ console.log(value); that.amount = value; },0); } }
大部分代码都是为了只能输入小数且精确到2位
而解决输入框值不会变的问题则是依赖
setTimeout(function(){ that.amount = value; },0);
目前尚无回复