修改jquery.validate提示错误方法,将错误信息用弹出框提示
<script src="@Url.Content("~/Scripts/jquery.validate.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"></script>
1 $.extend($.validator.defaults, { 2 showErrors: function (errorMap, errorList) { 3 var msg = ""; 4 $.each(errorList, function (i, v) { 5 msg += (v.message + "\r\n"); 6 }); 7 if (msg != "") 8 alert(msg); 9 } 10 });
修改后,发现执行无效果,并不会弹出错误信息,信息依然在文本框的后面显示
经调试发现,自定义的方法并没有扩展到 $.validator 中,
而要想重写的方法起作用,必须在引用
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"></script>
之前重写才可以。
完整代码如下:
1 <script src="@Url.Content("~/Scripts/jquery.validate.js")"></script> 2 3 <script type="text/javascript"> 4 $.extend($.validator.defaults, { 5 showErrors: function (errorMap, errorList) { 6 var msg = ""; 7 $.each(errorList, function (i, v) { 8 msg += (v.message + "\r\n"); 9 }); 10 if (msg != "") 11 alert(msg); 12 } 13 }); 14 </script> 15 <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"></script>
时间: 2024-10-13 11:01:02