Javascript 将textboxfor设置为接受2位小数

Javascript 将textboxfor设置为接受2位小数,javascript,jquery,asp.net-mvc,html.textboxfor,Javascript,Jquery,Asp.net Mvc,Html.textboxfor,我有这两个textboxfor,我需要它们只允许输入2位小数。 我假设jQuery或Javascript事件可以做到这一点。我只是不知道如何在Javascript中调用textboxfor。 任何帮助都将不胜感激 @Html.TextBoxFor(m => Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitCost, new { @disabled = "disabled", @on

我有这两个textboxfor,我需要它们只允许输入2位小数。 我假设jQuery或Javascript事件可以做到这一点。我只是不知道如何在Javascript中调用textboxfor。 任何帮助都将不胜感激

@Html.TextBoxFor(m => Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitCost,
                 new { @disabled = "disabled", @onchange = "costUpdate(this);", @cstType = "perUnit",@id="perUnit"})

<span class="sideLabel">@Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitDescription</span>

if (Model.Categories[c].EstimateGroups[g].EstimateItems[i].Units != null
 && !Model.Categories[c].EstimateGroups[g].EstimateItems[i].IsBasedOnHomeSquareFootage)
{ 
    @Html.TextBoxFor(m => Model.Categories[c].EstimateGroups[g].EstimateItems[i].Units,
                     new { @disabled = "disabled", @onchange = "costUpdate(this);", @cstType = "units" })
}
@Html.TextBoxFor(m=>Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitCost,
新的{@disabled=“disabled”、@onchange=“costUpdate(this);”、@cstType=“perUnit”、@id=“perUnit”})
@Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitDescription
if(Model.Categories[c].EstimateGroups[g].EstimateItems[i].Units!=null
&&!Model.Categories[c].EstimateGroups[g].EstimateItems[i].IsBasedOnHomeSquareFootage)
{ 
@Html.TextBoxFor(m=>Model.Categories[c].EstimateGroups[g].EstimateItems[i].Units,
新的{@disabled=“disabled”,@onchange=“costUpdate(this);”,@cstType=“units”})
}
您可以使用该属性。这就行了

[RegularExpression(@"^\d+.\d{0,2}$", 
             ErrorMessage = "It cannot have more than one decimal point value")]
public double PerUnitCost {get;set;}   

{0,2}
中,0和2表示小数点后的最小和最大数字。因此,根据您的要求进行更改。

+1非常简单的方法。虽然查询描述很复杂。@PKKG,但我下次会尽量解释得更好。@PKKG我是说,查询描述。