【问题标题】:jqueryUI - how to use a confirm dialog like a native JS confirm()?jqueryUI - 如何使用像原生 JS 确认()这样的确认对话框?
【发布时间】:2013-08-03 12:53:10
【问题描述】:

我正在使用 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 confirm() 我会这样使用:

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

但这种方式不适用于我的函数,因为我得到的只是“未定义”。

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

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

谢谢大家 问候

【问题讨论】:

标签: jquery dialog return return-value


【解决方案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
               }
           }
        ],
    });
    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多