需求:只能输入数字
问题:这种写法 能限制只能输入数字,其他字符不能输入,但是如果输入中文字符之后,v-model绑定的值不再更新了,再次输入的数字字符不再绑定到当前值上
<el-input
v-model.number="ruleForm.comment"
clearable
maxlength="5"
oninput="value=value.replace(/[^\d]/g,'')"
placeholder="请输入排序值(最大值99999)"
ref='inputTest'
/>
解决办法
方法一:
看了源码得知当 isOnComposition === true 时,是不会去更新值的
增加@input方法 查看当前这个属性是否为true
if(inputTest.value.isOnComposition) {
ruleForm.value.comment = elem.currentValue
}
方法二:
监听 el-input 的 input 事件, 然后对双向绑定的值进行处理
<el-input
v-model="scope.row.sort"
placeholder="请输入排序值"
@input="(v) => (scope.row.sort = v.replace(/[^\d]/g, ''))"
/>