2

我只是在写一些代码,但遇到了这样的问题:

alertify.dialog("confirm").set(
{
    'labels':
    {
        ok: 'Personal',
        cancel: 'Share'
    },
    'message': 'Select target:',
    'onok': function()
    {
        alertify.confirm($("#dir_select_user").get(0), function()
        {
            var i = $("#dir_select_user .dir_selector").val();
            t.find(".move_des").val(i);
            t.find(".move_verify").val("1");
            t.submit();
        }).set('labels',
        {
            ok: alertify.defaults.glossary.ok,
            cancel: alertify.defaults.glossary.cancel
        });
    },
    'oncancel': function()
    {
        alertify.confirm($("#dir_select_share").get(0), function()
        {
            var i = $("#dir_select_share .dir_selector").val();
            t.find(".move_des").val(i);
            t.find(".move_verify").val("1");
            t.submit();
        }).set('labels',
        {
            ok: alertify.defaults.glossary.ok,
            cancel: alertify.defaults.glossary.cancel
        });
    }
})   }).show();

我使用来自http://alertifyjs.comalertifyjs的库(不是来自http://fabien-d.github.io/alertify.js/)。

如果您尝试此代码,您会发现“onok”和“oncancel”对话框在选择personal或后很快消失share

这里有什么问题?我该如何解决?

4

1 回答 1

1

您的问题的根源是您试图在关闭时再次显示相同的对话框。默认的 AlertifyJS 对话框都是单例的(始终只有一个实例)。

您有 2 个解决方案:

  1. 延迟显示第二个确认,直到第一个确认实际关闭。

    alertify.confirm("confirm ? ", 
        function onOk() {
            //delay showing the confirm again 
            //till the first confirm is actually closed.
            setTimeout(function () {
                alertify.confirm("confirm another time ?");
            }, 100);
        },
        function onCancel() {
            //no delay, this will fail to show!
            alertify.confirm("this will not be shown!");
        }
    );
    
  2. 创建自己的瞬态(多实例)确认,只需从现有的继承即可。

    // transient (multi-instance)
    alertify.dialog('myConfirm', function factory(){ return {};},true,'confirm');
    alertify.myConfirm("confirm ? ", function(){
        alertify.myConfirm("confirm another time ?");
    });
    

    注意:请注意这一点,因为每次调用瞬态对话框都会创建一个新实例,您可以保存对已启动实例的引用并重新使用它们!

在此处查看演示。

于 2015-02-02T14:55:54.220 回答