【问题标题】:jQuery UI dialog won't open in Internet ExplorerjQuery UI 对话框不会在 Internet Explorer 中打开
【发布时间】:2012-07-12 09:40:58
【问题描述】:

我有一个 jQuery UI 对话框:

$("#dialog").dialog({
    modal: true,
    closeOnEscape: false,
    resizable: false,
    autoOpen: false,
    open: function() {
        $(".ui-dialog-titlebar").hide();
    }
});

我试图在 AJAX 调用之前打开此对话框。它可以在 Firefox 中使用,但在 IE 中无法打开,除非我在打开对话框之后输入alert。谁能告诉我可能是什么问题?我正在使用以下代码:

$("button").click(function() {
    $("#dialog").dialog('open');
    //alert('test'); //if I put this alert, the dialog will open
    $.ajax({
        type: "POST",
        url: "testing.txt",
        async: false,
        dataType: "text",
        success: function(returnText) {
            $("#dialog").dialog('close');
            $("#textarea").text(returnText);
        },
        error: function() {
            $("#dialog").dialog('close');
        }
    });
});

【问题讨论】:

    标签: ajax jquery-ui internet-explorer dialog


    【解决方案1】:

    open 事件由于潜在的动画而异步完成,因此可能发生的情况是由于 IE 的 JavaScript 解释速度慢,successerror 回调中关闭对话框的代码(它们也是异步的) ) 执行得离open 足够近,以至于您不会注意到对话框正在打开。我猜你的 AJAX 调用完成得很快。

    解决此问题的一种方法是将您的 AJAX 调用放在 setTimeout 块中。

    $("button").click(function() {
        $("#dialog").dialog('open');
    
        setTimeout(function() {
            $.ajax({
                type: "POST",
                url: "testing.txt",
                async: false,
                dataType: "text",
                success: function(returnText) {
                    $("#dialog").dialog('close');
                    $("#textarea").text(returnText);
                },
                error: function() {
                    $("#dialog").dialog('close');
                }
            });
        }, 1);
    });
    

    这将简单地将$.ajax 调用排队,这将允许open 事件完成。 John Resig 写了一篇很好的文章,说明了为什么这种事情在这里有效 - http://ejohn.org/blog/how-javascript-timers-work/

    【讨论】:

    • 谢谢,解决了我的问题!!我的弹出窗口中也有一个 GIF 动画,但它不起作用。请问您知道这个问题可能是什么吗?
    • 它没有理由不工作。您能否查看 Chrome 开发工具中的 Network 选项卡或 Firebug 中的 Net 选项卡,以查看图像是否被正确请求和返回?这可能是需要预先获取图像的问题,您可以通过在 JavaScript 早期的某处调用 new Image('path/to/myImage.png'); 来完成。
    【解决方案2】:

    解决此问题的另一种方法是将“dialog.open”部分放在 mousedown 事件中,而不是单击。这样,当您将其放入 setTimeout 时,您仍然可以执行 IE(8) 不喜欢的操作,例如下载文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      相关资源
      最近更新 更多