直接指定输入框的type为number不能控制不让输入小数点,经过查找发现自定义指令可以,以下是代码
<el-input type="number" v-enterNumber v-model="createdOrder.quantity" class="order-smail-input" @change="detailQuantity"></el-input>
钩子函数:
directives: {
enterNumber:{
inserted(el, binding, vnode, oldVnode){
el.addEventListener("keypress",function(e){
e = e || window.event;
let charcode = typeof e.charCode === 'number' ? e.charCode : e.keyCode;
let re = /\d/;
if(!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey){
if(e.preventDefault){
e.preventDefault();
}else{
e.returnValue = false;
}
}
})
}
}
},