页面:

<input v-model="xxx" v-focus/>

方法:

directives: {
    // 注册一个局部的自定义指令 v-focus
    focus: {
      // 指令的定义
      inserted: function(el, validateStr) {
        el.addEventListener("input", function() {
          //进行验证
          checkedfun(el);
        });
        function checkedfun(el) {
          let reg = new RegExp("^[0-9]*$");
          if (!reg.test(el.value)) {
            el.value = el.value.replace(/[^0-9]+/g, "");
            el.dispatchEvent(new Event("input"));//调用input事件使vue v-model绑定更新
          }
        }
      }
    }
  }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2021-07-21
相关资源
相似解决方案