vue限制input只能輸入正整數


解決方法:

使用 directives自定義vue標簽v-positive-int來監聽輸入框的值

<input v-positive-int  type="text" />
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: {
   },
 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM