Telerik MVC 货币文本框 - 自定义值格式

发布于 2024-12-24 02:22:47 字数 1115 浏览 5 评论 0原文

(首先,我已经在 Telerik 论坛上发布了此内容(链接),但该网站获得了更多流量,因此我希望尽快获得一些帮助。)

我正在尝试使用CurrencyTextBox 实现一些自定义值格式设置,但是控件处理值的方式使事情变得非常困难。

我想要做的是:如果用户输入一个不包含“.”的值。字符,它将将该值格式化为美分而不是美元。因此,如果用户输入“16”,我希望控件显示(并包含值)$0.16。如果用户输入带有“.”的值,我希望控件能够正常工作。如果用户输入“16.”、“16.0”或“16.00”,我希望控件显示(并包含值)“$16.00”。该应用程序将由零售业务人员使用,这就是他们期望价格值输入发挥作用的方式。

但是,无论我订阅 OnChange 事件还是订阅文本框本身的“blur”事件,它都会删除“.”。如果后面没有任何内容或者后面只有 0,则为字符。示例:如果我输入值“16.”、“16.0”或“16.00”,则从 javascript 中的文本框提取的值将为“16”。因为我必须检查“.”是否存在。字符,这打破了我正确格式化该值的逻辑。

所以我正在寻找一些帮助或建议。这是我现在可以根据需要正确格式化值的 javascript。正如您所看到的,如果该值不包含“.”字符,值除以100,这正是我想要的。剥离这个角色正在扼杀我的逻辑。 :)

$('custom-price').find('input').live('blur', function (e) {
    var sender = $(this).data('tTextBox');
    var priceVal = $(this).val(); // $(this).attr('value') also returns the value with the character stripped, FYI
    if (priceVal.indexOf(".") == -1) {
        priceVal = priceVal / 100;
    }
    sender.value(priceVal);
});

(First off, I have already posted this on the Telerik forums (link), but this site gets more traffic, so I'm hoping to get some help sooner.)

I am trying to achieve some custom value formatting with the CurrencyTextBox, but the way that the control handles values is making things decidedly hard.

What I want to do is this: if the user enters a value that does not contain a "." character, it will format that value as cents instead of dollars. So if a user enters "16", I want the control to display (and contain a value of) $0.16. If the user enters a value with a ".", I would like the control to function as normal. If the user enters "16.", "16.0" or "16.00", I want the control to display (and contain a value of) "$16.00". This application will be used by people in the retail business and this is how they expect inputs for price values to function.

However, whether I subscribe to the OnChange event or even the "blur" event on the textbox itself, it is stripping the "." character if there is either nothing after it or only 0's after it. Example: If I enter in a value of "16.", "16.0" or "16.00", the value pulled from the textbox in javascript will be "16". And since I have to check for the presence of the "." character, this breaks my logic to properly format the value.

So I'm looking for some help or suggestions. Here is the javascript that I have right now to properly format the value as required. As you can see, if the value does not contain a "." character, the value is divided by 100, which is exactly what I want. Stripping this character is killing my logic. :)

$('custom-price').find('input').live('blur', function (e) {
    var sender = $(this).data('tTextBox');
    var priceVal = $(this).val(); // $(this).attr('value') also returns the value with the character stripped, FYI
    if (priceVal.indexOf(".") == -1) {
        priceVal = priceVal / 100;
    }
    sender.value(priceVal);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软的没边 2024-12-31 02:22:47

对“.”使用ascii 代码进行同样的尝试。操作员(46)。 OnKeyPress 如果您发现 ascii 代码 46 更改控制格式。

问候,
达瓦尔·舒克拉

Try the same using ascii code for "." operator (46). OnKeyPress if you find ascii code 46 change the format for control..

Regards,
Dhaval Shukla

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文