- 只能输入0-100的正整数
- 最小值~最大值联动
<el-input
style="width: 70px"
:formatter="formatInput"
:parser="parseInput"
@blur="checknum('min', inputData, 'value1', inputData.value2)"
v-model.number="value"
placeholder="最小值"
/>
~~
<el-input
style="width: 70px"
:formatter="formatInput"
:parser="parseInput"
@blur="checknum('max', inputData, 'value2', inputData.value1)"
v-model.number="value"
placeholder="最大值"
/>
const formatInput = (value) => {
return value.replace(/^0+(\d)|[^\d]+/g, '')
}
const parseInput = (value) => {
let numValue = parseInt(value, 10)
if (isNaN(numValue)) return ''
return Math.max(0, Math.min(100, numValue)).toString()
}
const checknum = (type, inputData, field, otherValue) => {
let numValue = parseInt(inputData[field], 10) || 0
if (type == 'max') {
if (numValue < otherValue) {
numValue = otherValue
}
} else {
console.log(numValue, otherValue)
if (otherValue != null && numValue > otherValue) {
numValue = otherValue
}
}
inputData[field] = numValue
return numValue
}