所以...我有一个网站here ...

jQuery对话框正在向here发送ajax请求。

当您到达页面时,会自动弹出。请解散那个。

当您单击此图像时...


与此函数调用相关的...

$(function() {
    $("#compliance").dialog({
        autoOpen: true,
        modal: true,
        width: 750,
        height: 'auto',
        show: 'fade',
        hide: 'fade',
        position: {my: "center top", at:"center top", of: window },
        buttons: {
            "Dismiss": function() {
                $(this).dialog("close");
            }
        }
    });
    $(".dialogify").on("click", function(e) {
        e.preventDefault();
        $("#compliance").html("");
        $("#compliance").dialog("option", "title", "Loading...").dialog("open");
        $("#compliance").load(this.href, function() {
            $(this).dialog("option", "title", $(this).find("h1").text());
            $(this).find("h1").remove();
    });
    });
});


还是这个...


与此功能相关的...

$(function() {
    $("#switch").dialog({
        autoOpen: false,
        modal: true,
        width: 750,
        height: 'auto',
        show: 'fade',
        hide: 'fade',
        position: {my: "center top", at:"center top", of: window },
        buttons: {
            "Dismiss": function() {
                $(this).dialog("close");
            }
        }
    });
    $(".dialogify").on("click", function(e) {
        e.preventDefault();
        $("#switch").html("");
        $("#switch").dialog("option", "title", "Loading...").dialog("open");
        $("#switch").load(this.href, function() {
            $(this).dialog("option", "title", $(this).find("h1").text());
            $(this).find("h1").remove();
    });
    });
});


...一个模态出现但是似乎出现了两种模态。不透明的背景比应有的要暗。而且,当您关闭第一个时,还有另一个,因为背景变浅了。

为什么是这样?我只有一个函数调用。

最佳答案

好了,您有两个显示对话框的事件处理程序,并且都由相同的动作(单击任何.dialogify)触发。因此,这两个处理程序都试图处理对两个.dialogify元素的单击。单击任何一个都会导致两个对话框都出现(尽管它们加载相同的内容,因为this.href对于每个单击目标都是唯一的)。您可以通过在第一个处理程序内放置alert("a")并在第二个处理程序内放置alert("b")来确认这一点。

取而代之的是,只需使用一个选择器来唯一地标识每个事件的点击目标即可解决问题。

关于javascript - jQuery对话框模态在单击时被调用两次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18904792/

10-09 20:06
查看更多