12345678910111213 |
- import Vue from 'vue';
- // 输入的只能是数字
- Vue.directive('numericOnly', {
- bind(el) {
- // 找到 el 下的 input 标签
- const input = el.tagName === 'INPUT' ? el : el.querySelector('input');
- input.addEventListener('input', (e) => {
- // 如果输入的不是数字,则替换为空
- e.target.value = e.target.value.replace(/[^\d]/g, '');
- });
- },
- });
|