jquery - 需要有日期掩码,具体取决于 jquery 中的文化

标签 jquery asp.net-mvc html-helper culture

MVC 页面的文化取决于用户。在“搜索”选项(可以通过 DueDate 完成搜索)中,我需要在 DueDate 文本框上有一个掩码。面具必须依赖于用户的文化。在 js 和 cshtml 中都有错误提示:mask.split is not a function。将我的 jquery.maskedinput-1.2.3.jsmake.split 更改为 make.toString().split,错误消失了,但必须看起来像这样:[object Object] 或有一些 01 数字。任何想法? cshtml 中的代码如下所示:

   <script type="text/javascript">
    $(function () {
        var maskFormat = @Html.CurrentDateMask();
        $(".DateBox").mask(maskFormat);
    });
   </script>

最佳答案

您可以将 maskFormat 作为全局变量(将其放入 window 对象中),然后将其引用到 JS 文件中的 window.maskFormat :

cshtml 文件:

(function () {
    window.maskFormat = "@Html.CurrentDateMask();";
})();

并在您的 Javascript 文件中:

$(function(){
    $(".DateBox").mask(window.maskFormat);
});

但要小心,不要添加许多全局变量或使用易于覆盖的名称

关于jquery - 需要有日期掩码,具体取决于 jquery 中的文化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13952280/

相关文章:

javascript - json 字符串没有转换成对象

jquery - 使用 AngularJS,我如何根据操作有条件地添加类?

c# - 在带有 Telerik 的 Razor Web 帮助器中使用 html 帮助器

c# - 将 onblur 事件添加到 ASP.Net MVC Html.TextBox

jquery - toggle div 会填满 IE 中的剩余空间,但不会填满 google chrome 或 firefox

javascript - AngularJS Controller 不工作

asp.net-mvc - 按 PK 与用户名性能搜索用户

asp.net-mvc - 数据类型客户端验证在 MVC 2 中不起作用

asp.net-mvc-3 - 创建类似于Helper.BeginForm()的MVC3 Razor Helper

javascript - 在 JavaScript 或 jQuery 中获取部分 View 中的 HTML 类名称