如何插入jQuery的code?未捕获的Ref​​erenceError:$没有考虑到剃须刀code定义 [英] How to insert jQuery code? Uncaught ReferenceError: $ is not defined in view razor code

查看:138
本文介绍了如何插入jQuery的code?未捕获的Ref​​erenceError:$没有考虑到剃须刀code定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Asp.Net MVC 4剃刀$ C $在文件的结尾℃。

  ....
@section脚本{
    @ Scripts.Render(〜/包/ jqueryval)
}<脚本类型=文/ JavaScript的>
    $('#ADDRESS_STATE)VAL(MA)。
< / SCRIPT>
//////文件结束

然而,它产生下面的HTML code。而且在该行 $('#ADDRESS_STATE')引发错误VAL(MA); 。错误消息是未捕获的Ref​​erenceError:$没有定义。如何插入jQuery的code在剃刀文件?

  .....
<脚本类型=文/ JavaScript的>
    $('#ADDRESS_STATE)VAL(MA)。 //未捕获的Ref​​erenceError:$没有定义
< / SCRIPT>
            < /节>
        < / DIV>
        <页脚GT&;
            < D​​IV CLASS =内容包装>
                < D​​IV CLASS =浮动左>
                    < P>&安培;复印件; 2013 - Paperspeed< / P>
                < / DIV>
            < / DIV>
        < /页脚>        <脚本的src =/脚本/ jQuery的-1.9.1.js>< / SCRIPT>
    <脚本的src =/脚本/ jquery.unobtrusive-ajax.js>< / SCRIPT>
<脚本的src =/脚本/ jquery.validate.js>< / SCRIPT>
<脚本的src =/脚本/ jquery.validate.unobtrusive.js>< / SCRIPT>
    < /身体GT;
< / HTML>

更新:

以下是_Layout.cshtml的最后四行。

  @ Scripts.Render(〜/包/ jQuery的)
        @RenderSection(脚本,必需:false)
    < /身体GT;
< / HTML>


解决方案

您需要包括JQuery的之前,你使用它。

要实现这一点的常用方法是在你的主人布局包括您需要在头部的脚本。或放置一个可选部分(头),您可以有条件地包含脚本

首先创建一个包需要为您的jQuery的脚本你想在每一页上:

 公共静态无效RegisterBundles(BundleCollection包)
{
    bundles.Add(新ScriptBundle(〜/ JS /要求)。包括(
        〜/脚本/ jQuery的-2.0.2.js));    //其他包
}

然后创建一个网站模板:

 <!DOCTYPE HTML>
< HTML LANG =ENGT&;
< HEAD>
    <间的charset =UTF-8/>
    <标题>标题< /标题>
    @ Styles.Render(〜/ CSS /要求)
    @RenderSection(头,FALSE)//对CSS的逐页的基础页面上
< /头>
<身体GT;
    @RenderBody()
    @ Scripts.Render(〜/ JS /要求)//这里添加您的jQuery包括
    @RenderSection(脚本,FALSE)//这里你在页面的底部添加脚本
< /身体GT;
< / HTML>

然后用这个剃刀文件作为基地布局的所有其他衍生剃须刀的观点是这样的:

  @ {
    ViewBag.Layout =〜/查看/共享/ _MasterLayout.cshtml
}@section头{
     //你想添加的任何CSS
}< P>一些HTML内容< / P>@section脚本{
    你想//脚本在逐页的基础页面上的底部包括
}

I have the following Asp.Net MVC 4 razor code at the end of the file.

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

<script type="text/javascript">
    $('#Address_State').val('MA');
</script>
////// End of file

However, it generated the following html code. And it raises error at the line $('#Address_State').val('MA');. The error message is Uncaught ReferenceError: $ is not defined. How to insert jQuery code in the razor file?

.....
<script type="text/javascript">
    $('#Address_State').val('MA'); // Uncaught ReferenceError: $ is not defined
</script>
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; 2013 - Paperspeed</p>
                </div>
            </div>
        </footer>

        <script src="/Scripts/jquery-1.9.1.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>


    </body>
</html>

Update:

The following is the last four lines of _Layout.cshtml.

        @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html>

解决方案

You need to include JQuery before you actually use it.

A common way to achieve this is in your master layout include your require scripts in the head. Or place an optional section (head) where you can conditionally include scripts

First create a required bundle for your jquery scripts you want on every page:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/js/required").Include(
        "~/Scripts/jquery-2.0.2.js"));

    //other bundles
}

Then create a site template:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Title</title>
    @Styles.Render("~/css/required")
    @RenderSection("head", false) //For css on a page by page basis
</head>
<body>
    @RenderBody()
    @Scripts.Render("~/js/required") //Here add your jquery include
    @RenderSection("scripts", false) //Here you add scripts at the bottom of the page
</body>
</html>

Then use this razor file as the base layout for all other derived razor views like this:

@{
    ViewBag.Layout = "~/Views/Shared/_MasterLayout.cshtml";
}

@section head {
     //Any css you want to add
}

<p>Some html content</p>

@section scripts {
    //scripts you want to include at the bottom on a page by page basis
}

这篇关于如何插入jQuery的code?未捕获的Ref​​erenceError:$没有考虑到剃须刀code定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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