0

我有一个 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');
        }
    });
});
4

2 回答 2

4

由于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/

于 2012-07-12T10:28:09.563 回答
1

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

于 2012-07-27T17:42:34.643 回答