【发布时间】:2019-06-15 15:02:42
【问题描述】:
我正在使用以下代码动态生成模态弹出窗口,但遇到了问题。问题是模态显示正确,但我无法关闭它。我试过data-dismiss="modal" 和.modal("hide") 都没有成功。我检查了按钮点击事件是否被触发,唯一的问题是模式没有关闭。
JS
// Requires jQuery
var dialogHelper = new dialog();
function dialog() {
/* Bootstrap Modal Dialog
* Displays message from parameter
*/
this.ShowModalDialog = function (message, title, buttons) {
var dialogMessage = "";
var dialogTitle = "System Message";
var dialogButtons = [];
if (message)
dialogMessage = message;
if (title)
dialogTitle = title;
if (buttons) {
dialogButtons = buttons;
}
var id = randomString(10);
jQuery("<div/>", {
id: id,
class: "modal fade",
// href: 'http://google.com',
//title: title,
//rel: 'external',
//text: message
})
.attr("tabindex", "-1")
.attr("role", "dialog")
.attr("aria-labelledby", id + "Label")
.attr("aria-hidden", true)
.attr("data-backdrop", "static")
.attr("data-keyboard", false)
.load("/Static/BootstrapDialogTemplate.html", function () {
$("#" + id + " .modal-title")
.attr("id", id + "Label")
.text(dialogTitle);
$("#" + id + " .modal-body").text(dialogMessage);
var footer = $("#" + id + " .modal-footer");
dialogButtons.forEach(function (element) {
$('<button/>', {
text: element.Text,
click: function () {
if (element.Event) {
element.Event();
}
},
class: element.Class
})
.attr("data-dismiss", "modal")
.appendTo(footer);
});
})
.appendTo(document.body)
.modal("show");
};
};
/* Automatically destroy modal on close */
$(".modal").on('hidden.bs.modal', function () {
$(this).data('bs.modal', null);
});
function randomString(length) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = length;
var randomstring = '';
for (var i = 0; i < string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
};
/Static/BootstrapDialogTemplate.html
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
</div>
</div>
</div>
示例调用
dialogHelper.ShowModalDialog("Are you sure you want to create this item?", null, [{
Text: "Yes",
Event: function () { alert("YES!"); },
Class: "btn btn-primary"
}, {
Text: "No",
Event: function () { },
Class: "btn btn-secondary"
}]);
样本输出
<div id="2tF5r5mecT" class="modal fade show" tabindex="-1" role="dialog" aria-labelledby="2tF5r5mecTLabel" data-backdrop="static" data-keyboard="false" aria-modal="true" style="display: block;">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="2tF5r5mecTLabel">System Message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to create this item?</div>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal">Yes</button><button class="btn btn-secondary" data-dismiss="modal">No</button></div>
</div>
</div>
</div>
<div class="modal-backdrop fade show"></div>
通过随机测试,我设法推断出在生成我的模式时删除fade 类可以让我在不进行任何其他更改的情况下关闭它。
来自
jQuery("<div/>", {
id: id,
class: "modal fade",
// href: 'http://google.com',
//title: title,
//rel: 'external',
//text: message
})
到
jQuery("<div/>", {
id: id,
class: "modal",
// href: 'http://google.com',
//title: title,
//rel: 'external',
//text: message
})
我的代码有效,但没有淡入淡出效果。我已经检查了我所有的自定义 CSS 是否有 .fade,但我没有。当问题仍然存在时,浏览器上没有控制台错误。大家有没有遇到过这个问题?我刚刚升级到 JQuery 3.3.1 和 Bootstrap 4.2.1。当没有淡入淡出效果时感觉很奇怪。
普伦克
https://next.plnkr.co/edit/71QNigcwmUombEQ8?open=lib%2Fscript.js&preview
【问题讨论】:
-
在您的示例中,您应该像这样创建您的 div:
jQuery("<div></div>")以确保兼容性。另外,尝试在属性对象中引用"class"属性。根据文档,这必须被引用,因为class是保留字。 -
@jake2389 感谢您的评论,但代码按原样运行。唯一需要担心的是,当我将
fade添加为类时,淡入淡出动画会生效,但即使有data-dismiss="modal"或$("#id").modal("hide"),我也无法关闭模式。您可以从示例生成的模式中看到,除了该行为之外没有任何问题。
标签: javascript jquery bootstrap-4 bootstrap-modal