码上敲享录 > vue.js常见问题详解 > vue限制input只能输入正整数

vue限制input只能输入正整数

上一章章节目录下一章 2019-12-18已有6258人阅读 评论(0)

vue限制input只能输入正整数


解决方法:

使用 directives自定义vue标签v-positive-int来监听输入框的值

1.标签

<input v-positive-int  type="text" />


2.vue.js代码

 export default{

   name: 'Txt',

   data(){

     return {}

   },

   computed: {

   },

   methods: {

   },

   directives: {

   positiveInt: {

     bind: function(el) {

       el.handler = function() {

         el.value = Number(el.value.replace(/\D+/, ''))

       }

       el.addEventListener('input', el.handler)

     },

     unbind: function(el) {

       el.removeEventListener('input', el.handler)

     }

   }

 },


   components: {

   },

 }


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
1

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交