【问题标题】:Jquery dialog isn't working in MVCJquery 对话框在 MVC 中不起作用
【发布时间】:2013-06-20 06:00:12
【问题描述】:

现在我知道以前有人问过它,但我似乎无法让 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 个警报。有什么想法吗?

【问题讨论】:

    标签: jquery asp.net-mvc jquery-ui jquery-ui-dialog


    【解决方案1】:

    它只能自己找到:http://jsfiddle.net/Dy2Xw/

    检查您的脚本以确保它们正确加载。此外,您似乎正在加载 jquery 基础 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 错误,只输入你需要的,然后再试一次。

    【讨论】:

    • 清理它,我发现它可以与 JSFIDDLE 一起使用,但我不明白为什么它在我的应用程序中不起作用。我很确定脚本加载正确,因为如果不是,那么 HELLO 警报将不起作用。
    • 没有错误顺便说一句,但是如果您看到我编辑的版本,如果我将对话脚本放在警报 #2 和 #3 之间,它似乎没有达到警报 #3。有什么想法吗?
    • @Claud25 alert 是包装在 jquery ready 函数中的基本 js。与dialog 不同,它不需要 jquery ui 即可工作。检查脚本是否正确加载并删除任何不必要的脚本。在您的代码示例中,您有两个不同版本的 jquery 加载和两个不同版本的 jquery ui 加载。清理这些。你不需要每个两个。清理css。
    • 如何准确检查它们是否正确加载?因为我要放弃了。现在我只包含两个脚本,就像我编辑的版本中显示的那样。任何关于我应该在脑海中的建议,因为此时我将忘记它。
    • @Claud25 尝试直接链接到 CDN 的 &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt; 和 &lt;script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"&gt;&lt;/script&gt;。对于对话框所需的 css,请在 link 标签中使用它:http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/themes/base/jquery-ui.css。这应该会消除您以后可以发现的任何加载错误。
    【解决方案2】:

    也许这与 &lt;/body&gt; 与 &lt;head&gt; 标记之前的 js 文件的位置有关。将@Scripts.Render("~/bundles/jquery") 移动到&lt;head&gt;&lt;/head&gt; 标记

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      相关资源
      最近更新 更多