2

现在我知道以前有人问过它,但我似乎无法让 JQUERY 对话框工作。

我有一个从 JQUERY 网站获得的简单示例,但对话框从未弹出。

我的 _Layout.cshtml 页面

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>@ViewBag.Title - My ASP.NET MVC Application</title>
            <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
            <meta name="viewport" content="width=device-width" />
            <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
            <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
            @Styles.Render("~/Content/css")
            @Scripts.Render("~/bundles/modernizr")
        </head>
        <body>
            <header>
                <div class="content-wrapper">
                    <div class="float-left">
                        <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                    </div>
                    <div class="float-right">
                        <section id="login">
                            @Html.Partial("_LoginPartial")
                        </section>
                        <nav>
                            <ul id="menu">
                                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                                <li>@Html.ActionLink("About", "About", "Home")</li>
                                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <div id="body">
                @RenderSection("featured", required: false)
                <section class="content-wrapper main-content clear-fix">
                    @RenderBody()
                </section>
            </div>
            <footer>
                <div class="content-wrapper">
                    <div class="float-left">
                        <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                    </div>
                </div>
            </footer>

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

我的 Index.cshtml 页面

    <div id="dialog" title="Basic dialog">
        <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
    </div>

<script type="text/javascript">
    $(function () {
        alert("HELLO");
    });
</script>

    <script type="text/javascript">
        $(function () {
            $("#dialog").dialog();
        });
    </script>

警报弹出,但对话框没有,知道为什么吗?

编辑:做了一些编辑,现在我有了这个。

<div id="dialog" title="Basic dialog">
    <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

<script type="text/javascript">
    $(function () {
        alert("HELLO");
    });
</script>

<script type="text/javascript">
    $(function () {
        alert("HELLO2");
    });
</script>

<script type="text/javascript">
    $(function () {
        $("#dialog").dialog();
    });
</script>

<script type="text/javascript">
    $(function () {
        alert("HELLO3");
    });
</script>

现在,当我有三个警报时,它会发出 HELLO 和 HELLO2 警报,但它永远不会发出 HELLO3 .. 但是,如果我将 HELLO3 脚本放在对话框脚本之上,它将显示所有 3 个警报。有什么想法吗?

4

2 回答 2

1

它可以自己找到:http: //jsfiddle.net/Dy2Xw/

检查您的脚本以确保它们正确加载。此外,您似乎正在加载 jquery base js 两次。

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>

<script src="http://code.jquery.com/jquery-1.8.2.js "></script>

您还有两个 css 文件 - 不知道为什么需要这两个文件:

<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/themes/base/jquery.ui.dialog.css")" rel="stylesheet" type="text/css" />

清理那些脚本加载,检查控制台是否有任何 js 错误,只输入你需要的,然后再试一次。

于 2012-11-05T02:46:20.237 回答
0

也许它与</body>vs<head>标签之前的 js 文件的位置有关。移动@Scripts.Render("~/bundles/jquery")<head></head>标签

于 2013-04-16T05:27:21.093 回答