要求input只能输入正整数
用的el-input-number,发现不能实现input,因为拿到的值要做操作,所以最后选择了el-input结合正则来做
<el-input
class="jzinput
type="number"
style="width:85%"
v-model="scope.row.manuallyDays"
@input="changeDay($event,scope.row)"
@mousewheel.native.prevent
/>
changeDay(e,row) {
let str = e;
let len1 = str.substr(0, 1);
//1.如果第一位是0,后面输入任何数字都直接代替
if (str.length > 1 && len1 == 0) {
str = str.substr(1, 1);
}
//2.第一位不能是.
if (len1 == ".") {
str = "";
}
//3.最后进行正则替换,去掉所有的小数位
str = str.replace(/[^\d^]+/g, ""); // 保留数字和小数点
row.manuallyDays = str
if (row.manuallyDays > Number(row.days)) {
this.$message.error("手动调整后运行天数不能大于运维天数");
row.manuallyDays = row.systemDays;
this.tableKey = new Date()+''
}
},
去除input后面箭头
input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{
-webkit-appearance: none !important;
-moz-appearance: none !important;
-o-appearance: none !important;
-ms-appearance: none !important;
appearance: none !important;
margin: 0;
}
input[type="number"]{
-webkit-appearance:textfield;
-moz-appearance:textfield;
-o-appearance:textfield;
-ms-appearance:textfield;
appearance:textfield;
}
虽然输入框尾部的上下箭头去掉了,但是,当我们鼠标焦点在输入框中时,上下滚动鼠标滚轮,这时输入框中的数字会发生变化,容易导致误操作,这时只需要将el-input元素改成如下即可。
<el-input type="number" v-model.number="numberData" @mousewheel.native.prevent></el-input>