vue2.x directive - limit input can only enter a positive integer

Original link: https://www.mk2048.com/blog/blog.php?id=h0jahhkjbcab&title=vue2.x+directive+-+%E9%99%90%E5%88%B6input%E5%8F%AA%E8 % 83% BD% E8% BE % 93% E5% 85% A5% E6% AD% A3% E6% 95% B4% E6% 95% B0

onlyNum.js

import Vue from 'vue'
//只对input生效
export default function (el) {
    var input = el;
    input.onkeyup = function (e) {
        if(input.value.length==1){
            input.value = input.value.replace(/[^1-9]/g,'');
        }else{
            input.value = input.value.replace(/[^\d]/g, "");
        }
    };
}

main.js

import limitNum from './directive/onlyNum'

Vue.directive('limitNum', limitNum);

Just add v-limitNum will be able to enter into force on the input of the component.


More professional front-end knowledge, make the [2048] ape www.mk2048.com

Guess you like

Origin blog.csdn.net/whiteGay/article/details/102741601