我正在使用以下代码动态生成模式弹出窗口,但遇到了一个问题。问题是模式显示正确,但我无法关闭它。我都试过了
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;
};
/静态/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