对输入数字大小做限制

1、需求

     对一个input框所输出的数字大小做限制,如大于0和小于100。

2、思路

     普通的html可借助onkeyup,即键盘按键起来之后对所输入的数字进行判断。

3、语法

     3.1、如果是html5的话可以使用以下

<input type="number" min="0" max="100" />

    3.2、如果是普通的html,则借助js

<input type="text" id="input" />
<script>
    var input = document.getElementById("input");
        input.value = val;
        input.onkeyup = function(){
              this.value=this.value.replace(/\D/g,'')
              if( this.value < 0){
                  input.value = 0;
              }
              if(this.value > 100){
                    input.value = 100;
              }
         }
</script>

猜你喜欢

转载自blog.csdn.net/m_S_L/article/details/86573417