2014-06-12 52 views
-3

changeI有两个数字输入,并希望根据第一个数字的更改限制第二个数字的最大值或最小值。所以....使用javascript更改html数字最小值和最大值

<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> 

如何真正改变这些值是我在哪里卡住

感谢您的建议

+1

小心分享wh在你与我们一起尝试?如果将A更改为B,则不要给出任何规则。 –

+0

你在使用Jquery吗? – AntoineLev

+0

你知道如何选择你想改变的元素吗?如果没有,你应该从这里开始:https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById你也可能想检查如何改变元素属性的文档...没有更多关于究竟是什么导致了混乱,你不会获得更多的信息。 – scrappedcola

回答

0
$(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

+0

非常感谢! – user3648646