如何删除jQueryUI创建的对话框上的关闭按钮(右上角的X)?
当前回答
这是针对jQuery UI 1.12的。我为“classes”选项添加了以下配置设置
classes: {
'ui-dialog-titlebar-close': 'hidden',
},
整个对话框初始化如下所示:
ConfirmarSiNo(titulo, texto, idPadre, fnCerrar) {
const DATA_RETORNO = 'retval';
$('confirmar-si-no').dialog({
title: titulo,
modal: true,
classes: {
'ui-dialog-titlebar-close': 'hidden',
},
appendTo: `#${idPadre}`,
open: function fnOpen() { $(this).text(texto); },
close: function fnClose() {
let eligioSi = $(this).data(DATA_RETORNO) == true;
setTimeout(function () { fnCerrar(eligioSi); }, 30);
},
buttons: {
'Si, por favor': function si() { $(this).data(DATA_RETORNO, true); $(this).dialog("close"); },
'No, gracias': function no() { $(this).data(DATA_RETORNO, false); $(this).dialog("close"); }
}
});
}
我使用以下脚本调用来显示它:
ConfirmarSiNo('Titulo',
'¿Desea actualizar?',
idModalPadre,
(eligioSi) => {
if (eligioSi) {
this.$tarifa.val(tarifa.tarifa);
this.__ActualizarTarifa(tarifa);
}
});
在Html主体中,我有以下包含对话框的div:
<div class="modal" id="confirmar-si-no" title="" aria-labelledby="confirmacion-label">
mensaje
</div>
最终结果是:
函数“ConfirmarSiNo”基于帖子中的“Whome”答案。如何在Jquery UI对话框中实现“确认”对话框?
其他回答
您可以使用以下代码删除关闭按钮。还有其他的选择,你可能会觉得有用。
$('#dialog-modal').dialog({
//To hide the Close 'X' button
"closeX": false,
//To disable closing the pop up on escape
"closeOnEscape": false,
//To allow background scrolling
"allowScrolling": true
})
//To remove the whole title bar
.siblings('.ui-dialog-titlebar').remove();
$(".ui-button-icon-only").hide();
由于我发现我在应用程序的几个地方都在做这件事,所以我把它包装在一个插件中:
(function ($) {
$.fn.dialogNoClose = function () {
return this.each(function () {
// hide the close button and prevent ESC key from closing
$(this).closest(".ui-dialog").find(".ui-dialog-titlebar-close").hide();
$(this).dialog("option", "closeOnEscape", false);
});
};
})(jQuery)
用法示例:
$("#dialog").dialog({ /* lots of options */ }).dialogNoClose();
http://jsfiddle.net/marcosfromero/aWyNn/
$('#yourdiv'). // Get your box ...
dialog(). // ... and turn it into dialog (autoOpen: false also works)
prev('.ui-dialog-titlebar'). // Get title bar,...
find('a'). // ... then get the X close button ...
hide(); // ... and hide it
这里还有一个选项,就是使用CSS,它不会覆盖页面上的每个对话框。
CSS
.no-close .ui-dialog-titlebar-close {display: none }
HTML
<div class="selector" title="No close button">
This is a test without a close button
</div>
Javascript。
$( ".selector" ).dialog({ dialogClass: 'no-close' });
工作示例