Kendo NumericTextBox 在初始渲染时显示小数。

huangapple go评论74阅读模式
英文:

Kendo NumericTextBox shows decimals on inital render

问题

我在我的.cshtml页面上有以下的kendo NumericTextBox:

@(Html.Kendo().NumericTextBox()
   .Name("txtFrmSettMemoFDTFee")
   .Max(100)
   .Min(0)
   .Decimals(0)
   .Format("#")
   .HtmlAttributes(new { style = "width:100%" })
)

我使用以下的javascript代码加载框的初始值:

console.log("FDTFee = " + jsonData[0].FDTFee)
 
$("#txtFrmSettMemoFDTFee").kendoNumericTextBox({
    value: jsonData[0].FDTFee
});

当我查看控制台时,我看到这个:

FDTFee = 20

但当元素渲染时,它看起来像这样:

Kendo NumericTextBox 在初始渲染时显示小数。

如何让框的初始渲染显示20而不是20.00?

另外,当我增加/减少值时,框的格式会更改为整数格式(如果我从20.00减少到19)。但一旦我点击离开元素,整数值就会变成小数值(19变成19.00)。

谢谢!
1: https://i.stack.imgur.com/joihw.jpg

英文:

I have the following kendo NumericTextBox on my .cshtml page:

@(Html.Kendo().NumericTextBox()
   .Name("txtFrmSettMemoFDTFee")
   .Max(100)
   .Min(0)
   .Decimals(0)
   .Format("\\#")
   .HtmlAttributes(new { style = "width:100%" })
)

I load the initial value of the box with this javascript code:

console.log("FDTFee = " + jsonData[0].FDTFee)
 
$("#txtFrmSettMemoFDTFee").kendoNumericTextBox({
    value: jsonData[0].FDTFee
});

When I look at the console, I see this:

FDTFee = 20

But when the element is rendered, this is what it looks like:

Kendo NumericTextBox 在初始渲染时显示小数。

How can I get the initial rendering of the box to show a value of 20 and not 20.00?

Also, when I increase/decrease the value, the format of the box changes to an integer format (if I go from 20.00 down to 19). But as soon as I click off the element, the integer value changes to a decimal value (19 changes to 19.00).

Thank you!

答案1

得分: 3

I believe the primary problem is here:

$("#txtFrmSettMemoFDTFee").kendoNumericTextBox({
    value: jsonData[0].FDTFee
});

The MVC wrapper instantiates the widget for you but you are instantiating it all over again in javascript using jquery syntax when you try to set the value.

To set the value, get a reference to the already instantiated widget and call the value() method on it:

var numerictextbox = $("#txtFrmSettMemoFDTFee").data("kendoNumericTextBox");
numerictextbox.value(jsonData[0].FDTFee);

Link to Kendo UI NumericTextBox value method

Having done that, use format n0 and restrict decimals as noted in the comments to achieve the desired end result.

英文:

I believe the primary problem is here:

$("#txtFrmSettMemoFDTFee").kendoNumericTextBox({
    value: jsonData[0].FDTFee
});

The MVC wrapper instantiates the widget for you but you are instantiating it all over again in javascript using jquery syntax when you try to set the value.

To set the value, get a reference to the already instantiated widget and call the value() method on it:

var numerictextbox = $("#txtFrmSettMemoFDTFee").data("kendoNumericTextBox");
numerictextbox.value(jsonData[0].FDTFee);

https://docs.telerik.com/kendo-ui/api/javascript/ui/numerictextbox/methods/value

Having done that, use format n0 and restrict decimals as noted in the comments to achieve the desired end result.

huangapple
  • 本文由 发表于 2023年8月11日 02:44:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定