jquery全球化 – 不能让jquery全球化工作

前端之家收集整理的这篇文章主要介绍了jquery全球化 – 不能让jquery全球化工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试使用jQuery全球化插件,以修复 jquery无阻碍客户端验证的逗号问题.然而,我尝试了许多解决方案,没有很好的解决方案来解决这个问题.我在非英语本地化电脑上,这很重要,我的客户输入十进制值,如“123,66”而不是“123.66”. ASP.NET验证告诉我,价格必须是一个数字!呃你是认真的吗 ?大声笑

当我尝试修复时,我收到这个JavaScript错误.

$.global is undefined

这里我的代码

Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>

    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"> </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/jquery.form.js")"type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/glob.fix.js")" type="text/javascript"></script>
</head>

<body>
    @RenderBody()
</body>
</html>

glob.fix.js

$.validator.methods.range = function (value,element,param) {
   var globalizedValue = value.replace(",",".");
   return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}

$.validator.methods.number = function (value,element) {
   return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}

我不明白..它应该工作,因为我添加了〜/ Scripts / globalize.js.

任何想法?或者您可能有更好的解决方案来让客户端验证工作,并让我以十进制值输入逗号?

解决方法

我找到了最终摆脱十进制问题与逗号分隔符的方式!

这里有一张图片的结果!没有更多的验证问题.

修复的步骤.

1-获得Globalization library for jQuery

你必须得到最新的脚本!我也发现那里的答案已经过时了.调用库的对象不再是$.global或任何类似的,而是Globalize.

2-在项目中包含脚本.你必须在jquery.validation之后添加它们.

<script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>

3-替换验证器的某些方法.这将覆盖导致问题的’number’和’range’验证的方法.

$.validator.methods.number = function (value,element) {
    return this.optional(element) || !isNaN(Globalize.parseFloat(value));
} 

$.validator.methods.range = function (value,param) {
    return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
}

Globalize.parseFloat =>这实际上将替换包含’,’到’的任何东西.如果你选择了需要它的文化.

之后..你必须添加.这将使全球化的功能与文化一起工作.

$(document).ready(function () {
        Globalize.culture('fr-CA');

        // Only there to show which culture are being used.
        console.log(Globalize.culture().name);
});

完整的代码看起来像…

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"> </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.form.js")"type="text/javascript"></script>


<script type="text/javascript">

    $.validator.methods.number = function (value,element) {
        return this.optional(element) || !isNaN(Globalize.parseFloat(value));
    } 
    $.validator.methods.range = function (value,param) {
        return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
    }

    $(document).ready(function () {
        Globalize.culture('fr-CA');

        // Only there to show which culture are being used.
        console.log(Globalize.culture().name);
    });
</script>

</head>

<body>
    @RenderBody()
</body>
</html>
原文链接:https://www.f2er.com/jquery/180056.html

猜你在找的jQuery相关文章