zl程序教程

您现在的位置是:首页 >  其他

当前栏目

uniapp input输入框只能输入正整数demo效果(整理)

输入 效果 整理 Demo input 只能 输入框 uniapp
2023-09-14 09:04:05 时间
<input type="number" v-model="sum" @input="click" />
 
    export default {
        data() {
            return {
                sum: 0,
            }
        },
        methods: {
            click(e){
                const v = e.detail.value
                this.sum = '1'
        
                const zero = /^(0{2,})|[^0-9]/g
                let final = 0
                if (!v) {
                  final = 0
                } else {
                  final = v.toString().replace(zero, (v) => {
                    return 0
                  })
        
                  if (final.split('')[0] * 1 === 0) {
                    final = final.slice(1) - 0 || 0
                  }
                }
                this.$nextTick(() => {
                  this.sum = final.toString() || '0'
                })
            },
        }
    }
 

感谢您的支持
在这里插入图片描述