javascript 未捕获的类型错误:无法设置未定义的属性“unobtrusive”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17616759/
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-10-27 08:58:38  来源:igfitidea点击:

Uncaught TypeError: Cannot set property 'unobtrusive' of undefined

javascriptjqueryasp.net-mvcjquery-validateunobtrusive-validation

提问by Chris

I am using the latest MVC framework and bundles.

我正在使用最新的 MVC 框架和包。

The default "jqueryval" bundle that MVC creates is causing a javascript error

MVC 创建的默认“jqueryval”包导致了 javascript 错误

The scripts on my page are output like so

我页面上的脚本是这样输出的

<!-- In head -->
<script src="/Scripts/modernizr-2.6.2.js"></script>
<script src="/Scripts/jquery-2.0.2.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="/Content/Misc.js"></script>


<!-- Bottom of doc -->
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

My Bundle:

我的捆绑:

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

What is going wrong?

出了什么问题?

回答by Ian

In your bundle, you set it to include jquery.validate*and jquery.unobtrusive*, which would normally work. But if you look at your output, not all files that should be there are there. The point is that jquery.unobtrusive.validaterequires jquery.validateto be included (which, as you can see, is not, by your output). Apparently, the files aren't actually available in your project, and therefore aren't found by the bundler. Make sure to have all necessary files in your project so your bundler can find them and output them.

在您的包中,您将其设置为 includejquery.validate*jquery.unobtrusive*,这通常会起作用。但是,如果您查看输出,则并非所有应该存在的文件都在那里。关键是jquery.unobtrusive.validate需要jquery.validate包括在内(正如您所见,不是您的输出)。显然,这些文件实际上在您的项目中不可用,因此捆绑程序找不到。确保您的项目中有所有必需的文件,以便您的打包器可以找到它们并输出它们。

回答by ComeIn

I found that replacing the wildcards with full js script name worked.

我发现用完整的 js 脚本名称替换通配符有效。

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