废话不多说,直接上代码,记录一下
<el-form-item label="电价">
<el-input v-model="form.electricPrice" @keyup.native='keyupEvent($event,form.electricPrice)' @change="status=false" />
<span>¥/千瓦时</span>
</el-form-item>
keyupEvent()方法
keyupEvent(e){
e.target.value=e.target.value.replace(/[^\d.]/g, '');
e.target.value=e.target.value.replace(/\.{2,}/g, '.');
e.target.value=e.target.value.replace(/^\./g, '0.');
e.target.value=e.target.value.replace(/^\d*\.\d*\./g, e.target.value.substring(0,e.target.value.length-1));
e.target.value=e.target.value.replace(/^0[^\.]+/g, '0')
e.target.value=e.target.value.replace(/^(\d+)\.(\d\d).*$/, '$1.$2')
this.form.electricPrice=e.target.value
console.log(this.form.electricPrice)
}
网友评论