【发布时间】:2017-05-26 09:51:13
【问题描述】:
我有一个按钮,它执行以下操作:单击时,将打开一个模式弹出窗口。 在模态弹出窗口上还有另一个按钮,单击该按钮将显示另外 2 个文本框和 2 个按钮。我想要的是,当我单击关闭或简单地关闭模式时,我希望它处于包含 2 个标签和添加按钮的先前状态。
这些是我的 javascript 代码:
//this shows the additional textboxes and buttons
$( "#toggle" ).click(function()
{
$( "#content" ).toggle("slow");
$( "#divButtons" ).toggle();
$("#toggle").hide();
});
//this is where i tried to refresh the modal back to its original state
$(function () {
$(document).on("hidden.bs.modal", "#exampleModal", function () {
$(this).find("#content").remove(); // Remove from DOM.
$(this).find("#divButtons").remove();
$("#toggle").show();
$(this).find("#portValue").html("");
$(this).find("#hostValue").html("");
});
});
这是我到目前为止所做的:Demo
我的代码的问题是,当我关闭模式,然后再次单击添加按钮时,它会删除按钮并且 2 个文本框和 2 个按钮不显示。
【问题讨论】:
-
那是因为您在
"hidden.bs.modal"事件中使用$(this).find("#content").remove();这一行完全从 DOM 中删除了您的#content。因此,下次当您单击“添加新按钮”时,将不会显示#content,因为它不再存在。
标签: javascript jquery html bootstrap-modal