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

时间:2013-08-02 09:43:47

标签: 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 = $("#" + 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');
}

我从其他JS文件中调用该函数。 一个js确认()我会这样用:

if (confirm('confirm me')) doThis();

但是这种方式不能用于我的功能,因为我得到的只是一个“未定义”。

if (jqConfirm(paramstring...)) doThis();

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

谢谢大家 问候

1 个答案:

答案 0 :(得分:1)

你不能这样做。只有内置插件(alertconfirm等)才能真正停止在页面上执行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
               }
           }
        ],
    });
    // ...
}
相关问题