如何使用 Javascript 更改 HTML5 上输入类型编号的最大值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13613937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:17:20  来源:igfitidea点击:

How to change the max value for input type number on HTML5 with Javascript?

javascripthtml

提问by Alberto

I need change the max attribute of an input element with JavaScript. For example:

我需要使用 JavaScript 更改输入元素的 max 属性。例如:

<input type="number" name="miname" min="0" max="MyVar" value="0"/>

回答by Ibu

You can assign an id to your input :

您可以为您的输入分配一个 id :

<input id="myInput" type="number" name="miname" min="0" max="MyVar" value="0"/>

then access it via javascript:

然后通过javascript访问它:

var input = document.getElementById("myInput");
input.setAttribute("max",100); // set a new value;

Note you can also access it directly if your input is in a form

请注意,如果您的输入是在表单中,您也可以直接访问它

document.formName.miname.setAttribute("max",newValue);

回答by erickyi2006

sharing the angular way of doing it. Use [attr.max].

分享做这件事的角度方式。使用 [attr.max]。

<input matInput type="number" min="0"  [attr.max]="totalOrders"  placeholder="Total Skybridge Orders" formControlName="totalSkybridge" (change)="onTotalSkybridgeOrders($event)"