用javascript改变html数字最小值和最大值

Change html numeric min and max with javascript

本文关键字:最小值 最大值 数字 html javascript 改变      更新时间:2023-09-26

changi有两个数字输入,我想根据第一个数字的变化来限制第二个数字的最大值或最小值。所以…

 <input id="input1" type="number" min="0" max="10" value="5" change="OnChangeNumeric()" >
 <input id="input2" type="number" min="0" max="10" value="1" >
 <script>
 function OnChangeNumeric() {
 //how to actually change the values is where i am stuck
 $('#input2').max = 5;
 }
 </script>

如何实际改变值是我卡住的地方

谢谢你的建议

$(function() {
    $('#input1').on('change',function() {
        var thisVal = this.value;
        /*calculate newMin, and newMax for #input2 according to your rules
          based on thisVal ........
        $('#input2').attr('min', newMin).attr('max', newMax);*/
        console.log( $('#input2')[0] );
    });
});

不需要内联JS:

<input id="input1" type="number" min="0" max="10" value="5"/>
<input id="input2" type="number" min="0" max="10" value="1"/>

JS FIDDLE DEMO