jqueryUI-如何使用像本机JS confirm()一样的确认对话框?

jqueryUI-如何使用像本机JS confirm()一样的确认对话框?,jquery,dialog,return,return-value,Jquery,Dialog,Return,Return Value,我正在使用JQ对话框替换本机js confirm()函数。 为了把它作为一个中心函数,我把它放到了一个函数中 var dialogID = "dialog_" + createUUID(); var width = 300; var height = 150; var url = ""; var formObj = ""; function jqConfirm(dialogID,title,txt,width,height,url,formObj) { var dialog = $("#

我正在使用JQ对话框替换本机js confirm()函数。 为了把它作为一个中心函数,我把它放到了一个函数中

var dialogID = "dialog_" + createUUID();
var width = 300;
var height = 150;
var url = "";
var formObj = "";
function jqConfirm(dialogID,title,txt,width,height,url,formObj) {
    var dialog = $("#" + dialogID);
    // generate the HTML
    if (!$("#" + dialogID).length) {
        dialog = $('<div id="' + dialogID + '" style="display:hidden;" class="loading"></div>').appendTo('body');
}
dialog.dialog({
    bgiframe: true,
    autoOpen: false,
    width: width,
    height: height,
    minWidth:100,
    minHeight:100,
    maxWidth:980,
    maxHeight:700,
    modal: true,
    dialogClass: 'dialogWithDropShadow',
    resizable:false,
    draggable:false,
    show:'fade',
    hide:'fade',
    title: title,
    buttons: [
        {
            text: "OK",
            "class": 'mscbutton',
            click: function() {
                if (formObj.length) {
                $(formObj).submit();
            } else if (url.length) {
                document.location.href = url;
            }
        $(this).dialog('close');
            }
        },
        {
            text: "Cancel",
            "class": 'mscbutton',
            click: function() {
                $(this).dialog('close');
            return false;
            }
        }
    ],
});
// fill the dialog
$(dialog).html(txt);
dialog.dialog('open');
$(dialog).removeClass('loading');
}
但这种方法对我的函数不起作用,因为我得到的只是一个“未定义的”

对话框打开,工作正常,但似乎什么也没有返回。我知道我做错了什么。但是什么呢

谢谢大家
问候

你不能那样做。只有内置的(
alert
confirm
,等等)才能真正停止页面上的JavaScript执行,等待用户执行操作

相反,您必须使用回调,例如:

jqConfirm(paramstring, function(result) {
    if (result) {
        doThis();
    }
});
您可以使用按钮上的回调来触发传递到
jqConfirm
的回调:

// Add `callback` to this somewhere appropriate; I've just stuck it at the
// end. Consider using an options object rather than lots of individual
// parameters.
function jqConfirm(dialogID, title, txt, width, height, url, formObj, callback) {
    // ...
    dialog.dialog({
        // ...
        buttons: [
           {
               text: "OK",
               "class": 'mscbutton',
               click: function () {
                   if (formObj.length) {
                       $(formObj).submit();
                   }
                   else if (url.length) {
                       document.location.href = url;
                   }
                   $(this).dialog('close');
                   callback(true);            // <== Do the callback
               }
           },
           {
               text: "Cancel",
               "class": 'mscbutton',
               click: function () {
                   $(this).dialog('close');
                   callback(false);           // <== Do the callback
               }
           }
        ],
    });
    // ...
}
//在适当的地方添加'callback';我刚把它贴在了墙角
//结束。考虑使用一个选项对象而不是很多个人
//参数。
函数jqConfirm(dialogID、title、txt、宽度、高度、url、formObj、回调){
// ...
对话({
// ...
按钮:[
{
文字:“OK”,
“类”:“mscbutton”,
单击:函数(){
if(formObj.长度){
$(formObj.submit();
}
else if(url.length){
document.location.href=url;
}
$(this.dialog('close');
回调(true);//的可能重复项
jqConfirm(paramstring, function(result) {
    if (result) {
        doThis();
    }
});
// Add `callback` to this somewhere appropriate; I've just stuck it at the
// end. Consider using an options object rather than lots of individual
// parameters.
function jqConfirm(dialogID, title, txt, width, height, url, formObj, callback) {
    // ...
    dialog.dialog({
        // ...
        buttons: [
           {
               text: "OK",
               "class": 'mscbutton',
               click: function () {
                   if (formObj.length) {
                       $(formObj).submit();
                   }
                   else if (url.length) {
                       document.location.href = url;
                   }
                   $(this).dialog('close');
                   callback(true);            // <== Do the callback
               }
           },
           {
               text: "Cancel",
               "class": 'mscbutton',
               click: function () {
                   $(this).dialog('close');
                   callback(false);           // <== Do the callback
               }
           }
        ],
    });
    // ...
}