首页 > 解决方案 > 如何使输入文本显示格式为十进制格式 0.00

问题描述

我正在使用一个<input type="text">给员工的工作时间。我想将文本格式显示为0.00并使其仅接受两位数字和两位十进制数字作为十进制格式。

<td>
     <input type="text" asp-for="NormalHrs" class="col-sm-5" maxlength="5" onkeypress="return (event.charCode >= 48 && event.charCode <= 57) ||event.charCode == 46 || event.charCode == 0 " />
</td>

标签: htmlasp.net-core

解决方案


我们可以这样做这个js,

首先做一个事件(focusout/onclick你喜欢哪个)

输入类型应为“数字”

<input type="number" onfocusout="setTwoNumberDecimal(this)" step="0.01" value="0.00"/>

function setTwoNumberDecimal(el) {
        el.value = parseFloat(el.value).toFixed(2);
    };

推荐阅读