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: {
},
}