Vue.directive('enterNumber', { //全局方法 限制input輸入框只能輸入純數字 組件中調用如(<el-input v-enter-number v-model="number" ></el-input>)
inserted: function(el) {
el.addEventListener("keypress", function(e) {
e = e || window.event;
let charcode = typeof e.charCode === 'number' ? e.charCode : e.keyCode;
let re = /\d/;
if (!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
});
}
});
vue自定義指令,限制input輸入框只能輸入純數字
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.