Commit 334e469b authored by yaya's avatar yaya

feat:限制金额输入

parent 23504995
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<el-input v-model="goods.name"/> <el-input v-model="goods.name"/>
</el-form-item> </el-form-item>
<el-form-item label="价格" prop="price"> <el-form-item label="价格" prop="price">
<el-input v-model="goods.price" placeholder="0.00"> <el-input v-model="goods.price" type="number" placeholder="0.00" @input="limitInput" >
<template slot="append"></template> <template slot="append"></template>
</el-input> </el-input>
</el-form-item> </el-form-item>
...@@ -155,6 +155,17 @@ export default { ...@@ -155,6 +155,17 @@ export default {
this.init() this.init()
}, },
methods: { methods: {
limitInput(value) {
this.goods.price = ('' + value) // 第一步:转成字符串
.replace(/[^\d^\.]+/g, '') // 第二步:把不是数字,不是小数点的过滤掉
.replace(/^0+(\d)/, '$1') // 第三步:第一位0开头,0后面为数字,则过滤掉,取后面的数字
.replace(/^\./, '0.') // 第四步:如果输入的第一位为小数点,则替换成 0. 实现自动补全
.match(/^\d*(\.?\d{0,2})/g)[0] || '' // 第五步:最终匹配得到结果 以数字开头,只有一个小数点, 而且小数点后面只能有0到2位小数
},
init: function() { init: function() {
if (this.$route.query.id == null) { if (this.$route.query.id == null) {
return return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment