jQuery 如何插入jQuery代码?未捕获的 ReferenceError:$ 未在视图剃刀代码中定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15458523/
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
How to insert jQuery code? Uncaught ReferenceError: $ is not defined in view razor code
提问by ca9163d9
I have the following Asp.Net MVC 4 razor code at the end of the file.
我在文件末尾有以下 Asp.Net MVC 4 razor 代码。
....
@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?
但是,它生成了以下 html 代码。它会在行中引发错误$('#Address_State').val('MA');
。错误信息是Uncaught ReferenceError: $ is not defined
。如何在 razor 文件中插入 jQuery 代码?
.....
<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>© 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.
以下是_Layout.cshtml的最后四行。
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
回答by gdp
You need to include JQuery before you actually use it.
在实际使用之前,您需要包含 JQuery。
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
实现此目的的常用方法是在您的主布局中在头部包含您的 require 脚本。或者放置一个可选部分(head),您可以在其中有条件地包含脚本
First create a required bundle for your jquery scripts you want on every page:
首先为每个页面上所需的 jquery 脚本创建一个必需的包:
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:
然后使用这个 razor 文件作为所有其他衍生的 razor 视图的基本布局,如下所示:
@{
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
}
回答by James
Move the following lines of code to the top of your _Layout.cshtml view inside of the body tag. Then the jQuery scripts will load first.
将以下代码行移动到 body 标记内 _Layout.cshtml 视图的顶部。然后 jQuery 脚本将首先加载。
<body>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
回答by PSL
Your Script is called before the jquery is even loaded so it doesn't know what $ means. Try having the jquery scripts loaded in the head section of your layout file.
你的脚本在 jquery 加载之前被调用,所以它不知道 $ 是什么意思。尝试在布局文件的 head 部分加载 jquery 脚本。
回答by Muhammed Y?lmaz
jquery add is after the render body? you will move following "
jquery add 是在渲染体之后吗?你将跟随“
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
" lines to tags
" 到标签的行
like this "
像这样 ”
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</head>
"
”