简体   繁体   English

ASP.NET MVC 4 jQuery验证脚本捆绑包不起作用

[英]ASP.NET MVC 4 jQuery Validation Script Bundle Not Working

I have recentely upgraded a website to use ASP.NET MVC 4 and have the following code to render my jQuery validation bundle. 我已经对网站进行了一次升级以使用ASP.NET MVC 4,并使用以下代码来呈现我的jQuery验证包。

But I get the following error: 但是我收到以下错误:

Microsoft JScript runtime error: Object doesn't support property or method 'live'

And this error when clicking in a text box: 单击文本框时出现此错误:

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

My bundle code: 我的捆绑代码:

    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

My _Layout.cshtml code: 我的_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")

Here is my rendered HTML: 这是我呈现的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>

Can someone point out why this error might be occurring? 有人能指出为什么会出现这种错误吗?

Thanks, Alex. 谢谢,亚历克斯。

The .live method has been deprecated in jQuery v1.7+ and removed in jQuery v1.9+. .live方法已在jQuery v1.7 +中弃用,并在jQuery v1.9 +中删除。

Bugreport: http://bugs.jquery.com/ticket/13213 Bugreport: http ://bugs.jquery.com/ticket/13213
Explanation: http://jquery.com/upgrade-guide/1.9/#live-removed 说明: http//jquery.com/upgrade-guide/1.9/#live-removed

.live() removed .live()已删除

The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. 自jQuery 1.7以来,.live()方法已被弃用,并已在1.9中删除。 We recommend upgrading code to use the .on() method instead. 我们建议升级代码以使用.on()方法。 To exactly match $("a.foo").live("click", fn), for example, you can write $(document).on("click", "a.foo", fn). 要完全匹配$(“a.foo”)。live(“click”,fn),例如,你可以写$(document).on(“click”,“a.foo”,fn)。 For more information, see the .on() documentation. 有关更多信息,请参阅.on()文档。 In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality. 与此同时,jQuery Migrate插件可用于恢复.live()功能。

How to fix: Download the jquery migrate plugin and add it to your bundling: 如何修复:下载jquery migrate插件并将其添加到您的捆绑:

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

Update: The migrate plugin is now available from Nuget as well: 更新:现在可以从Nuget获得迁移插件:

PM> Install-Package jQuery.Migrate PM> Install-Package jQuery.Migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM