jQuery 1.9 MVC 4 项目中的 jquery.validate.js 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14659023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:49:37  来源:igfitidea点击:

Error in jquery.validate.js in MVC 4 Project with jQuery 1.9

jqueryjquery-validateunobtrusive-validation

提问by Eric J.

I created a new ASP.Net MVC 4 project using the template in Visual Studio 2012. After upgrading to jQuery 1.9, the login functionality breaks. Specifically, I get the error

我使用 Visual Studio 2012 中的模板创建了一个新的 ASP.Net MVC 4 项目。升级到 jQuery 1.9 后,登录功能中断。具体来说,我得到了错误

0x800a138f - JavaScript runtime error: Unable to get property 'call' of undefined or null reference

0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“调用”

at line 1172, column 5 in jquery.validate.js

在第 1172 行,jquery.validate.js 中的第 5 列

How can I fix this issue?

我该如何解决这个问题?

回答by Eric J.

This issue is fixed in jQuery Validation Plugin 1.11.0pre.

此问题已在jQuery 验证插件 1.11.0pre 中修复

Unfortunately there is currently no pre-release build on NuGet, so it is currently necessary to download jquery.validation.js directly from GitHub:

不幸的是,目前 NuGet 上没有预发布版本,因此目前需要直接从 GitHub 下载 jquery.validation.js:

jQuery.Validation 1.11is now available via NuGet (thanks @Simon_Weaver).

jQuery.Validation 1.11现在可以通过 NuGet 获得(感谢 @Simon_Weaver)。

UPDATE

更新

It looks like generalsupport for jQuery 1.9 is in the jQuery.Validation code base but not yet released

看起来对 jQuery 1.9 的一般支持在 jQuery.Validation 代码库中,但尚未发布

https://github.com/jzaefferer/jquery-validation/pull/613

https://github.com/jzaefferer/jquery-validation/pull/613

回答by Richard

If you install jQuery.Migrate 1.1.1 then it will work fine with jQuery.Validation 1.11, jQuery 1.9 and 2.x.

如果您安装了 jQuery.Migrate 1.1.1,那么它将与 jQuery.Validation 1.11、jQuery 1.9 和 2.x 一起正常工作。

As well as doing install-package jQuery.Migrate, you must then add the JavaScript to BundleConfig.cs - I added it just after jQuery and this is what the line that registers jQuery looks like now:

除了执行install-package jQuery.Migrate,您还必须将 JavaScript 添加到 BundleConfig.cs - 我在 jQuery 之后添加了它,这就是注册 jQuery 的行现在的样子:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
  "~/Scripts/jquery-{version}.js",           // jQuery itself
  "~/Scripts/jquery-migrate-{version}.js")); // jQuery migrate

回答by Travis J

The issue is the line of code inside of the validation script

问题是验证脚本中的代码行

$.event.handle.call(this,e);

The problem here is with $.event.handle. Both thisand ehave proper values. jQuery deprecated $.event.handle in favor of jQuery.event.dispatch according to the migration script http://code.jquery.com/jquery-migrate-1.2.0.js.

这里的问题在于$.event.handle. 双方thise有正确的价值观。根据迁移脚本http://code.jquery.com/jquery-migrate-1.2.0.js,jQuery弃用了 $.event.handle 以支持 jQuery.event.dispatch 。

Fixing these errors is as easy as simply replacing the reference to handle with dispatch.

修复这些错误就像简单地用 dispatch 替换要处理的引用一样简单。

This can be wrapped in a document.ready callback to ensure it runs after the other scripts run.

这可以包装在 document.ready 回调中,以确保它在其他脚本运行后运行。

$(function(){
    jQuery.event.handle = jQuery.event.dispatch
});

回答by Texellab

I had the same issue, no problem in firefox but ie still gives the error. I am using jquery-2.0.0; jQuery Validation Plugin 1.11.1

我有同样的问题,firefox 没问题,但 ie 仍然给出错误。我正在使用 jquery-2.0.0;jQuery 验证插件 1.11.1

I found a fix: replace

我找到了解决办法:替换

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

with

@section Scripts{
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryval")"></script>
}

回答by Javier

Replacing the bundles script path by the following line worked for me.

通过以下行替换包脚本路径对我有用。

 <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryval")"></script>

I tried updating the jquery validation via Nuget, and also updated the jquery to 2.0 version, but none of those solution worked. The only one that worked fine without downloading anything Pre version was the solution proposed by Texellab

我尝试通过 Nuget 更新 jquery 验证,并将 jquery 更新到 2.0 版本,但这些解决方案都不起作用。唯一一个无需下载任何东西就可以正常工作的 Pre 版本是 Texellab 提出的解决方案

回答by Vance

The underlying problem is that some methods that had been deprecated since jQuery 1.7 (when new, improved methods were added) have been removed in 1.9. jQuery Validate has, apparently, not been updated so it uses the new methods instead. I found that using the jQuery Migrate Plugin, which puts the removed methods back in, solved my problems with this.

潜在的问题是一些自 jQuery 1.7(添加新的改进方法时)以来已被弃用的方法已在 1.9 中删除。jQuery Validate 显然没有更新,因此它使用新方法。我发现使用 jQuery Migrate 插件(它将已删除的方法放回原处)解决了我的问题。

回答by Derek Davidson PST CST

Here's another solution:

这是另一个解决方案:

I used MVC 3 with jQuery 1.71 and jQuery UI 1.8.20. I used the jQuery UI Tabs widget and setup each tabbed area to load a partial page that contained input fields.

我在 jQuery 1.71 和 jQuery UI 1.8.20 中使用了 MVC 3。我使用 jQuery UI Tabs 小部件并设置每个选项卡区域来加载包含输入字段的部分页面。

Works great in Firefox v22 and Chrome v28, fails in IE10. The error I got was 'Unable to get property 'settings' of undefined or null reference'

在 Firefox v22 和 Chrome v28 中运行良好,在 IE10 中失败。我得到的错误是“无法获取未定义或空引用的属性‘设置’”

The resolution was to go to the partial forms and comment out the following code:

解决方案是转到部分表单并注释掉以下代码:

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

It now works successfully in all browsers.

它现在可以在所有浏览器中成功运行。