值由逗号[,]分隔的jQuery UI Slider和jQuery Globalization [英] jQuery UI Slider and jQuery Globalization with value delimited by comma [ , ]

查看:108
本文介绍了值由逗号[,]分隔的jQuery UI Slider和jQuery Globalization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发同时使用 jQuery全球化 jQuery UI滑块.

I'm developing an ASP.NET MVC 3 app that uses both jQuery Globalization and jQuery UI Slider.

我正在将该应用程序从巴西(pt-BR)本地化为葡萄牙语,并且还支持英语.

I'm localizing this app to Portuguese from Brazil (pt-BR) and it also supports English.

问题在于,当我将语言更改为葡萄牙语时,所有Model float值都由逗号(,)分隔,例如7,7.然后,我尝试设置Slider的初始值,但是它只接受带有点(.)的值,例如7.7.

The problem is that when I change the language to Portuguese, all Model float values come delimited by a comma ( , ) like 7,7. I then try to set the Slider initial value but it only accepts the value with a dot ( . ) like 7.7.

为了克服这种情况,我正在这样做:

To overcome such situation I'm doing this:

$.CreateSlider("#sliderHeight", "#Height", @Model.Height.FormatValue(), 0, 2.5, 0.01);

FormatValue是扩展方法:

FormatValue is an extension method:

public static string FormatValue(this float value)
{
    return value.Value.ToString().Replace(",", ".");
}

您看到我必须用点定界符替换逗号定界符,以便可以设置jQuery Slider值.

You see that I have to replace the comma delimiter with a dot delimiter so that I can set the jQuery Slider value.

然后我必须使用jQuery全球化通过这种方式在UI中显示正确的值:

I then have to use jQuery Globalization to show the correct value in the UI this way:

$(textBox).val($.global.format(ui.value, 'n2'));

至少有效...

我的问题是:

是否有更好的或标准的方法?

Is there a better or standard way of doing this?

问题是Model值正以其应有的方式出现,也就是说,当我将区域性切换为pt-BR时,数字以(,)分隔.在我看来,这是正确的.限制是jQuery Slider接收值的方式.

The thing is that the Model values are coming the way they should, that is, as I switched the culture to pt-BR the numbers come ( , ) delimited. This is correct in my point of view. The limitation is in the way the jQuery Slider receives the value.

只是为了补充:如果我尝试将值传递为它们来自@Model的方式,我将遇到类似这样的事情,显然会中断(因为参数值本身用逗号分隔):

Just to complement: if I tried to pass the values the way they come from the @Model, I'd have something like this, that obviously would break (because the parameters values are themselves separated by commas):

$.CreateSlider("#sliderHeight", "#Height", 7,7 , 0, 2.5, 0.01);

推荐答案

您的滑块接受InvariantCulture值,因此您需要提供此类值.只需修改您的方法:

Your slider accepts InvariantCulture values, so you need to provide such. Just modify your method:

public static string ParseValue(this float value)
{
    return value.Value.ToString(CultureInfo.InvariantCulture);
}

顺便说一句. ParseValue不是最佳名称,因为不进行任何解析. FormatValue听起来会更好.
顺便提一句.您的原始方法在某些区域性中无法正常工作(对于较小的值可能看不见,但对于大于100%的情况,可能会发生不好的情况).如果需要点作为浮点,请始终使用InvariantCulture.

BTW. ParseValue is not the best name here as no parsing going on. FormatValue would sound better.
BTW. Your original method won't work correctly for some cultures (might not be visible for small values but for greater than i.e. 100 bad things could happen). If you need a dot as floating point, always use InvariantCulture.

这篇关于值由逗号[,]分隔的jQuery UI Slider和jQuery Globalization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆