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

<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'
                })
            },
        }
    }
 

猜你喜欢

转载自blog.csdn.net/qq_38881495/article/details/128592435