js文件
let enterNumber = {
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;
}
}
});
}
}
export default enterNumber;
main.js中
import enterNumber from './directive/enterNumber'
Vue.directive('enterNumber',enterNumber)
vue页面中
<el-input v-enter-number v-model="number" ></el-input>
网友评论