Javascript 对于 input type="number" 如何将默认值设置为 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34924709/
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
for input type="number" how to set default value to 0
提问by NiksJ
For a textbox that accept numbers as shown below, how can the default value be set to 0?
对于如下所示的接受数字的文本框,如何将默认值设置为 0?
<input name="smtpPort" type="number" id="smtpPort" size="30" maxlength="50" class="input-full" tabindex="4" value="{{model.PortNumber}}">
Though the value in DB is null, it still shows 0 in textbox.
尽管DB中的值为空,但在文本框中仍然显示为0。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by zessx
HTML attribute to set a default value is value
:
设置默认值的 HTML 属性是value
:
<input type="number" value="1">
You're setting this default value to null
, and nothing (in HTML) can change it to zero (or anything else).
You must fix the value in your database, or in your template engine (something like {{model.PortNumber || 0}}
).
您将此默认值设置为null
,并且(在 HTML 中)没有任何东西可以将其更改为零(或其他任何内容)。
您必须修复数据库或模板引擎中的值(例如{{model.PortNumber || 0}}
)。
At least, you can force your field to be filled with required
attribute:
至少,您可以强制您的字段填充required
属性:
<input type="number" required value="">
回答by kravits88
<input name="smtpPort" type="number" id="smtpPort" size="30" maxlength="50" class="input-full" tabindex="4" value="{{model.PortNumber || 0}}">
I assume you're using AngularJS.
我假设您正在使用 AngularJS。