捆绑脚本不工作MVC [英] Bundled scripts not working MVC

查看:136
本文介绍了捆绑脚本不工作MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经捆绑JQuery验证脚本,如:

I have bundled jquery Validate scripts like :

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

和在_layout我作为渲染

and am rendering in the _Layout as

@Scripts.Render("~/bundles/jqueryval")

但是,当我尝试验证我的按钮点击的形式,如:

But when i try to Validate my form on button click like :

$("form").validate({
                    errorPlacement: function (error, element) {
                        error.insertAfter(element).css("display", "inline");
                    }
                });

它给我像错误:

0x800a01b6 - JavaScript的运行时错误:对象不支持属性或方法验证

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'validate'

因此​​,任何人建议我为这个问题。

So anyone suggest me for this problem.

推荐答案

运行时deferentiating从缩小的版本中未缩小的(基于标准JavaScript的命名约定),因此,如果您指定〜/脚本/ jquery.unobtrusive *路径说明将包括 jquery.unobtrusive.js 缩小时关闭和 jquery.unobtrusive。 min.js 缩小时开启。

The runtime deferentiating minified versions from un-minified(based on standard JavaScript naming conventions), so if you specify "~/Scripts/jquery.unobtrusive*" path the specifier will include jquery.unobtrusive.js when minification turned off and jquery.unobtrusive.min.js when minification turned on.

检查一下你有你的脚本文件夹jquery.unobtrusive.min.js和jquery.validate.min.js文件。

Check out if you have jquery.unobtrusive.min.js and jquery.validate.min.js files in your Scripts folder.

要打开微小手动 - 粘贴 BundleTable.EnableOptimizations = TRUE; 之后增加您的包

To turn on minification manually - paste BundleTable.EnableOptimizations = true; after adding your bundles.

编辑:

您的错误说validate方法还没有宣布。
如果你的 @ Scripts.Render(〜/包/ jqueryval)是在打电话之前位于 $(形式)。确认请查看()
要么
包验证的调用到的jQuery(回调)函数的是这样的:

Your error saying that validate method didn't declared yet. Please check out if your @Scripts.Render("~/bundles/jqueryval") is located before you call $("form").validate() or wrap calling of validate into jQuery(callback) function like this:

$(function(){
    $("form").validate({
                    errorPlacement: function (error, element) {
                        error.insertAfter(element).css("display", "inline");
                    }
                });
})

这将等到所有的文件被加载。

That's will wait until all document is loaded.

这篇关于捆绑脚本不工作MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆