我最近升级了一个网站以使用 ASP.NET MVC 4 并使用以下代码来呈现我的 jQuery 验证包。
但是我得到以下错误:
Microsoft JScript runtime error: Object doesn't support property or method 'live'
单击文本框时出现此错误:
Unhandled exception at line 1234, column 5 in http://localhost:13112/Scripts/jquery.validate.js
0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'call': object is null or undefined
我的捆绑代码:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
我的 _Layout.cshtml 代码:
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/modernizr")
这是我渲染的 HTML:
<link href="/Content/site.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.core.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.resizable.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.selectable.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.accordion.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.autocomplete.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.button.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.dialog.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.slider.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.tabs.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.datepicker.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.progressbar.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.theme.css" rel="stylesheet"/>
<script src="/Scripts/jquery-1.9.0.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/jquery-ui-1.9.2.js"></script>
<script src="/Scripts/modernizr-2.6.2.js"></script>
有人可以指出为什么会发生此错误吗?
谢谢, 亚历克斯。
最佳答案
.live 方法已在 jQuery v1.7+ 中弃用并在 jQuery v1.9+ 中删除。
错误报告:http://bugs.jquery.com/ticket/13213
解释:
http://jquery.com/upgrade-guide/1.9/#live-removed
.live() removed
The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .on() method instead. To exactly match $("a.foo").live("click", fn), for example, you can write $(document).on("click", "a.foo", fn). For more information, see the .on() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality.
如何修复: 下载 jquery migrate plugin并将其添加到您的 bundle 中:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-migrate-{version}.js"));
更新:迁移插件现在也可以从 Nuget 获得:
PM> Install-Package jQuery.Migrate
关于c# - ASP.NET MVC 4 jQuery 验证脚本包不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14357416/