【问题标题】:Alertify dialog disappeared before confirming在确认之前,警报对话框消失了
【发布时间】:2015-02-02 13:28:31
【问题描述】:

我刚刚写了一些代码,遇到了这样的问题:

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.com(而不是来自http://fabien-d.github.io/alertify.js/)的alertifyjs 库。

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

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

【问题讨论】:

  • 在 jsfiddle.net 上使用适当的 html 和 JS 制作一个示例,我们将看看可以做什么。

标签: javascript alertifyjs


【解决方案1】:

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

你有两个解决方案:

  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 ?");
    });
    

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

查看演示here

【讨论】:

  • 我很确定第一种方法更适合我。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多