我正在尝试替代javascript确认()。我找到了可以完全自定义的 jquerydialog() 函数。问题是我无法让它返回true or false.
这是我的代码:
$('#delBox').dialog(
{ autoOpen: false, resizable: false, modal: true, closeOnEscape: true, width: 300, height: 'auto', title: 'Deletar registro',
buttons: {
"Ok": function () {
return true;
}, "Cancelar": function () {
$(this).dialog("close");
return false;
}
},
open: function () {
var buttonsSet = $('.ui-dialog-buttonset').find("button:contains('Ok')");
buttonsSet.attr("class", "ui-button ui-state-default");
$('.ui-dialog-titlebar-close span').empty();
$('.ui-dialog-buttonset').find("button:contains('Ok')").button({
text: false,
icons: {
primary: 'ui-icon-ok'
}
});
$('.ui-dialog-buttonset').find("button:contains('Cancelar')").button({
text: false,
icons: {
primary: 'ui-icon-cancel'
}
});
}
});
这仅在选择任何选项之前返回一个对象:
function deletar() {
alert($('#delBox').dialog('open'));
}
jQueryUI 对话框无法返回true
or false
因为它们显示在其他内容之上但是without阻塞执行。
你能做的最好的事情是:
做盒子modal
这样它就隐藏了其他内容
根据选择的选项提供要使用的回调。
为了获得额外的奖励积分,您可以创建一个$.Deferred()
Promise 对象并在显示对话框时返回该对象。那么你可以resolve
or reject
按钮事件处理程序中的承诺。
这将使您能够清楚地分离显示对话框和执行由对话框随后触发的操作:
function showDialog() {
var def = $.Deferred();
// create and/or show the dialog box here
// but in "OK" do 'def.resolve()'
// and in "cancel" do 'def.reject()'
return def.promise();
}
showDialog().done(function() {
// they pressed OK
}).fail(function() {
// the pressed Cancel
});
// NB: execution will continue here immediately - you shouldn't do
// anything else now - any subsequent operations need to be
// started in the above callbacks.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)