输入类型= " number "如何将默认值设为0

时间:2021-07-24 17:42:44

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.

如有任何帮助,我们将不胜感激。

2 个解决方案

#1


8  

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中)都不能将它更改为0(或其他任何东西)。您必须在数据库或模板引擎(类似{model)中修复值。PortNumber | | 0 } })。

At least, you can force your field to be filled with required attribute:

至少,您可以强制您的字段填充必需的属性:

<input type="number" required value="">

#2


4  

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

#1


8  

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中)都不能将它更改为0(或其他任何东西)。您必须在数据库或模板引擎(类似{model)中修复值。PortNumber | | 0 } })。

At least, you can force your field to be filled with required attribute:

至少,您可以强制您的字段填充必需的属性:

<input type="number" required value="">

#2


4  

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