jQuery $(document).ready 在 MVC4 项目下不起作用

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

$(document).ready doesn't work under MVC4 project

jquery.netasp.net-mvc-4

提问by Developer

I am just inserting

我只是插入

$(document).ready(function () {
    alert("!!!");
});

as usually into

通常进入

@{
    ViewBag.Title = "Sign Up";
    Layout = "~/Views/Shared/_WebSite.cshtml";
}

<script type="text/JavaScript">

    $(document).ready(function () {
        alert("!!!");
    });

</script>

<h2>Sign Up</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
{

}

}

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

but I cannot see alert() message...

但我看不到 alert() 消息...

other things of jQueryare working fine at this page...

jQuery 的其他功能在此页面上运行良好...

What do I am missing?

我缺少什么?

Any clue?

有什么线索吗?

Thank you!

谢谢!

回答by Ufuk Hac?o?ullar?

You can see jQuery is referenced after your code if you inspect the html.

如果您检查 html,您可以看到在您的代码之后引用了 jQuery。

Default project comes with an optional scripts section that will be rendered after jQuery reference in the layout, that's where your code should go.

默认项目带有一个可选的脚本部分,它将在布局中的 jQuery 引用之后呈现,这就是您的代码应该去的地方。

@{
    ViewBag.Title = "Sign Up";
    Layout = "~/Views/Shared/_WebSite.cshtml";
}



<h2>Sign Up</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
{

}

}

@section Scripts {
    <script type="text/JavaScript">

    $(document).ready(function () {
        alert("!!!");
    });

    </script>
}