我怎么可以自定义ASP NET MVC不显眼的审定JQuery的消息? [英] How can I customize the unobtrusive validation JQuery messages in ASP NET MVC?

查看:91
本文介绍了我怎么可以自定义ASP NET MVC不显眼的审定JQuery的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,当它不显眼validtaion我想让它显示:

I have a form, and when it does the unobtrusive validtaion I want it to show:

此消息:请输入一个值的。

而不是此消息的:。此字段必填

我试着在我jquery.validate.unobtrusive.js的末尾添加此文件

I tried adding this at the end of my jquery.validate.unobtrusive.js file

(function ($) {
   $.extend($.validator.messages, {
     required: "Please enter a value."
   }); 
}(jQuery));

但它只是不工作。

But it's just not working.

我也试图直接修改在jquery.validate.js文件中的消息变量,但它不工作要么。

I also trying modifying directly the "messages" variable in jquery.validate.js file but it is not working either.

你能告诉我怎样才能得到这个工作?

Could you please tell me how can I get this done?

推荐答案

如果您使用的数据注解的属性和不希望自定义消息为每一个(其中,看来通过您的意见,这其中是这样),我可以看到两个选项:

If you're using the data annotation attributes and don't want to customize the message for each one (which, it appears by one of your comments that this is the case), I can see two options:

1)创建自己的数据标注属性,并设置一个默认的消息,并改变所有实例 [必需] 来的新属性。

1) Create your own data annotation attribute and set a default message, and change all instances of [Required] to your new attribute.

2)你可以尝试在jQuery的:

2) You can try it in jQuery:

// reset the form's validator
$("form").removeData("validator");

// change the text on each required attribute
$(":input[data-val-required]").attr("data-val-required", "Please enter a value");

// reapply the form's validator
$.validator.unobtrusive.parse(document);

我试过这个JavaScript在浏览器的控制台,一切似乎都工作正常。为什么你需要做的原因是因为:1)验证规则是由浏览器,所以你需要清除它们(见此缓存回答获得更多信息)。 2)所有的错误信息都在HTML标记,一旦呈现页面,所以的这就是的,您需要更改验证消息。

I tried this javascript in Chrome's console and everything seemed to work ok. The reason why you need to do this is because 1) the validation rules are cached by the browser so you need to clear them out (See this answer for more information). And 2) all of the error messages are in the html markup once the page is rendered, so that's where you need to change the validation messages.

这篇关于我怎么可以自定义ASP NET MVC不显眼的审定JQuery的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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