【问题标题】:How to remove JQuery dialog content after click on OK button or X(Close)?单击确定按钮或 X(关闭)后如何删除 JQuery 对话框内容?
【发布时间】:2023-03-04 07:00:01
【问题描述】:

我已经创建了创建对话框的 JQuery 函数。可以在函数中传递的参数很少,例如 HTML 内容、标题、宽度、高度。当我点击 OK 按钮或 X 按钮时,我开始更多地查看屏幕后面发生的事情。我看到对话框设置为display:none。所有内容都保留在屏幕上。在我单击下一个应该调用对话框的函数后,我看到创建了新的 HTML 内容。我想知道关闭后如何清除对话框的比赛?这是我的对话框功能:

$.extend({ 
    alert: function (message, title, height, width, print) {
        var dialogButtons = {
            "Ok": function() {
                $(this).remove();
            }
        };

        if (print) dialogButtons.Print = function() {
            $(this).dialog().printArea();
        };

        $("<div></div>").dialog( {
            buttons: dialogButtons,
            close: function (event, ui) { $(this).hide(); },
            resizable: false,
            title: title,
            modal: true,
            width: height,
            height: width,
            overflow:"auto",
            position: {
                    my: "center",
                    at: "center",
                    of: window
            }
        }).html(message);
    }
}); 

这是我如何调用此函数的示例:

<div id="container">
   Some HTML 
</div>

//Call that creates dialog box
var divID = $('#container').show();
$.alert(divID,'Form Input',800,600);

关闭对话框后,我在屏幕后面看到以下内容:

<div style="height: auto; width: 600px; top: 0px; left: 655.8px; z-index: 101; display: none;" tabindex="-1" role="dialog" class="ui-dialog ui-corner-all ui-widget ui-widget-content ui-front ui-dialog-buttons ui-draggable" aria-describedby="ui-id-1" aria-labelledby="ui-id-2"><div class="ui-dialog-titlebar ui-corner-all ui-widget-header ui-helper-clearfix ui-draggable-handle"><span id="ui-id-2" class="ui-dialog-title">Form Input</span><button type="button" class="ui-button ui-corner-all ui-widget ui-button-icon-only ui-dialog-titlebar-close" title="Close"><span class="ui-button-icon ui-icon ui-icon-closethick"></span><span class="ui-button-icon-space"> </span>Close</button></div><div id="ui-id-1" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: 468px; display: none;"></div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-corner-all ui-widget">Ok</button></div></div></div>

这是我再次单击将打开对话框的按钮后的内容:

<div style="height: auto; width: 600px; top: 0px; left: 655.8px; z-index: 101;" tabindex="-1" role="dialog" class="ui-dialog ui-corner-all ui-widget ui-widget-content ui-front ui-dialog-buttons ui-draggable" aria-describedby="ui-id-9" aria-labelledby="ui-id-10"><div class="ui-dialog-titlebar ui-corner-all ui-widget-header ui-helper-clearfix ui-draggable-handle"><span id="ui-id-10" class="ui-dialog-title">Form Input</span><button type="button" class="ui-button ui-corner-all ui-widget ui-button-icon-only ui-dialog-titlebar-close" title="Close"><span class="ui-button-icon ui-icon ui-icon-closethick"></span><span class="ui-button-icon-space"> </span>Close</button></div><div id="ui-id-9" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: 468px;"><div id="container" style="display: block;">
</div></div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-corner-all ui-widget">Ok</button></div></div></div>

这是对话框创建新 html 并将上一个设置为 display:none 的正常方式还是我的函数中有一些错误?

【问题讨论】:

  • 你的标题说删除对话框,你的问题说清除对话框的内容。是哪一个?
  • $(this).dialog("close") 是你要找的吗?
  • @Barmar 我尝试使用 .dialog("close");但页面上仍保留内容。
  • 这里对我有用:jsfiddle.net/barmar/bv3fgxbr/1
  • @Barmar 打开您的开发工具并尝试打开对话框。您将看到 html 创建对话框。单击确定后,将设置为显示:无。然后,如果您再次单击对话框,将创建新的 html 代码,而不是使用以前的对话框代码。我的问题是是否可以使用以前的代码而不是创建新的 html 代码?

标签: javascript jquery html dialog


【解决方案1】:

解决这个问题的常用方法是在 HTML 中添加一个 DIV,并使用它而不是动态地构造一个新的 DIV。它的 CSS 应该是 display: none;,所以在你显示对话框之前它是不可见的。

如果你不能这样做,你可以将 DIV 保存在一个变量中,然后重复使用它。

然后在不使用的时候使用$(this).dialog("close") 隐藏它。

var dialogDiv;
$.extend({ 
    alert: function (message, title, height, width, print) {
        var dialogButtons = {
            "Ok": function() {
                $(this).dialog("close");
            }
        };

        if (print) dialogButtons.Print = function() {
            $(this).dialog().printArea();
        };

        if (!dialogDiv) {
            dialogDiv = $("<div>"); 
        }   
        dialogDiv.dialog( {
            buttons: dialogButtons,
            resizable: false,
            title: title,
            modal: true,
            width: height,
            height: width,
            overflow:"auto",
            position: {
                    my: "center",
                    at: "center",
                    of: window
            }
        }).html(message);
    }
}); 

不需要close: 选项,因为关闭对话框是默认操作。

DEMO

【讨论】:

  • 我已经测试了您的答案并且工作正常。我没有看到为我的对话框创建的多个元素。请你能解释一下为什么我的原始代码每次都创建新的 div 吗?我看到您检查 div 是否存在,如果不创建新元素。那是我错过的那块吗?谢谢!
猜你喜欢
  • 2013-04-06
  • 1970-01-01
  • 2013-01-28
  • 2021-11-26
  • 2010-10-28
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
相关资源
最近更新 更多