directive.js 386 B

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