【问题标题】:jQuery UI dialog with boolean return - true or false带有布尔返回的 jQuery UI 对话框 - true 或 false
【发布时间】:2012-05-29 00:07:50
【问题描述】:

我正在尝试替换 javascript confirm()。我找到了可以完全自定义的 jquery dialog() 函数。问题是我不能让它返回 truefalse

这是我的代码:

$('#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'));
}

【问题讨论】:

    标签: javascript jquery user-interface dialog


    【解决方案1】:

    jQueryUI 对话框无法返回 truefalse,因为它们显示在其他内容之上,但没有阻止执行。

    你能做的最好的就是:

    1. 制作modal框,使其隐藏其他内容

    2. 根据选择的选项提供要使用的回调。

    对于额外的奖励积分,您可以创建一个$.Deferred() 承诺对象并在您显示对话框时返回它。然后,您可以在按钮事件处理程序中 resolvereject 承诺。

    这将使您在显示对话框和执行随后由它触发的操作之间清晰地分开:

    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.
    

    【讨论】:

    • 你认为函数应该返回完整的 Deferred 对象还是通过return def.promise() 受限的 Promise 接口?
    • @AtesGoral 是的,def.promise() 更可取 - 好点。
    • showDialog 中,我是否必须使用$.one 将事件绑定到OKCancel,即每次调用showDialog 时,该事件将被添加一次执行?有没有办法使用$.on,即绑定一次并重复使用showDialog
    • @buffer 你根本不需要显式绑定任何事件 - 只需在初始对话框选项中提供回调。
    • 对不起,我应该提到如果一个人不使用 jQuery UI(即有一个自定义模式对话框),事件可以只在 init 中添加一次,而不是在 showDialog 中重复添加?
    【解决方案2】:

    第一个答案很好 - 我想我只是添加一些代码来展示如何返回被点击的按钮:

    function ShowYesNoMessage(title, message) {
    var def = $.Deferred();
    $("#infoMessage").html(message);
    
    $("#dialog_infoMessage").dialog({
        modal: true,
        title: title,
        buttons: {
            Yes: function () {
                $("#infoMessage").html("");
                $(this).dialog("close");
                def.resolve("Yes");
            },
            No: function () {
                $("#infoMessage").html("");
                $(this).dialog("close");
                def.resolve("No");
            }
        }
    });
    return def.promise();
    }
    
    
    $.when(ShowYesNoMessage("Are you sure", message)).then(
                function(status) {
                    if (status == "Yes") {
                        //do the next step
                    }
                }
            );
    

    【讨论】:

    • 对于二元选择,我个人更喜欢.resolve / .reject 组合。哦,如果在 $.when 内部调用的函数返回一个承诺,则不需要 $.when
    猜你喜欢
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多