When the element ui input type is number, limit the size of the input value, only allow the input of numbers, prohibit the scroll wheel, and correct it after inputting a value greater than the set value

  • Do not enter non-digits
 onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
  • Prohibit the scroll wheel (this instruction can not be used for functions with a limited maximum value)
@mousewheel.native.prevent
  • limit minimum
:min="0"
  • limit maximum
参数说明
-item  用于修正值的引用对象
-['calendarValue', 'day]  item中的对象层级到最后修正的属性
-30 最大值

函数
limitMaxNumber(item, ['calendarValue', 'day'], 30)
  				  <!-- vue 部分-->
				  <el-input
                    type="number"
                    @mousewheel.native.prevent
                    v-model="item.calendarValue.day"
                    :min="0"
                    @change="limitMaxNumber(item, ['calendarValue', 'day'], 30)"
                    onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
                  >
                  </el-input>
  <!-- method 方法 -->
       limitMaxNumber(item, attributeArray, maxNumber) {
      // 操作对象
      let obj = { ...item };
      // 对象数组
      let arrayObj = [item];

      // 根据属性数组,遍历到底部,校验
      attributeArray.forEach((element, index) => {
        obj = obj[element];

        // 当遍历到最后一个元素
        if (index == attributeArray.length - 1) {
          // 当超过允许的最大值
          if (obj > maxNumber) {
            obj = maxNumber;
          }
        }
        // 保存到对象数组
        arrayObj.push(obj);
      });

      // 根据对象数组,倒遍历组装对象
      for (let i = arrayObj.length - 1; i > -1; i--) {
        if (arrayObj[i] instanceof Object) {
          // 当不是最后一个元素时,将当前元素对象作为属性,赋值为上个元素对象的属性
          if (i > 0) {
            arrayObj[i - 1][attributeArray[i - 1]] = arrayObj[i];
          }
        } else {
          //将当前值,赋值为上个元素对象的属性
          arrayObj[i - 1][attributeArray[i - 1]] = arrayObj[i];
        }
      }
    }

Guess you like

Origin blog.csdn.net/qq_39879542/article/details/121896963