【问题标题】:Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'未捕获的错误:无法在初始化之前调用对话框上的方法;试图调用方法“打开”
【发布时间】:2013-11-16 19:49:32
【问题描述】:

我正在使用 jQuery UI 将 ASP.NET MVC 5 部分视图呈现到一个空的 div 中:

$(document).ready(function () {
    $(".SiteName").on("click", function (event) {
        event.preventDefault();
        var siteName = $(this).data("sitename");
        //alert(siteName);
        $.ajax({
            url: "/EtlLog/ListSiteJobs?SiteDescription=" + encodeURIComponent(siteName),
            type: "GET",
        })
        .done(function (result) {
            $("#ListSite").html(result).dialog("open");
        });
    });
})

单击后检查 ListSite div,我看到它充满了预期的结果,但我得到一个 jQuery UI 错误:

Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'open' 

我尝试了 .ready() 的各种组合,甚至将正在渲染的 div 嵌套在用于 .dialog() 的 div 中。必须查看有关此错误的所有问题,但仍未找到解决方案。

【问题讨论】:

    标签: javascript jquery asp.net-mvc jquery-ui asp.net-mvc-5


    【解决方案1】:

    您首先需要“设置”对话框,然后是open。试试:

    $(document).ready(function () {
        $(".SiteName").on("click", function (event) {
            event.preventDefault();
            var siteName = $(this).data("sitename");
            //alert(siteName);
            $("#ListSite")
               .load("/EtlLog/ListSiteJobs?SiteDescription=" + encodeURIComponent(siteName))
               .dialog({autoOpen:true})
        });
    })
    

    PS:我冒昧地使用load()缩短了您的代码。

    【讨论】:

    • 完美,谢谢!但是为什么对话框是透明的?我期待默认...
    • 没有任何线索说明为什么它在您当前的代码示例中是透明的,因为它们都与样式无关。完成标准步骤。确保包含 jquery ui css 文件。确保您的控件没有无效的 html 或被破坏的内联 css 规则。确保其他示例 ui 对话框在您的站点等中正常工作。
    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2014-03-07
    • 2014-05-29
    • 2019-10-02
    相关资源
    最近更新 更多