【问题标题】:Javascript variable not setting correctly for first timeJavascript变量第一次没有正确设置
【发布时间】:2014-04-10 12:27:47
【问题描述】:

我的问题是我的变量 flag 没有得到更新,我什至尝试使用 setTimeout 函数来做到这一点,但没有运气!

JavaScript

function confirmed(msg, okFtn, cnclFtn){
    flag=false;
    $(document.createElement('div')).html('\
        <div style="max-width:250px;width:100%;">'+msg+'\
        <p>\
            <button value="yes" class="button small gradient blue left rnd5 cnfrm">OK</button>\
            <button value="no" class="button small gradient blue left rnd5 cnfrm">Cancel</button>\
        </p></div>')
        .modal({
            onShow: function (dialog) {
                $('.simplemodal-close').remove();
                $("#simplemodal-container").css({'height': 'auto','width':'auto'});
                $(window).trigger('resize.simplemodal'); 
            },
            onClose: function(){
                console.log(flag);
                if(flag)
                    return true;
                else
                    return false;
            }
        });
    $('button.cnfrm').click(function(){
        flag=true;
        if ($(this).val() == 'yes') {
            if(okFtn && typeof okFtn === 'function')
                okFtn();
        } else {
            if(cnclFtn && typeof cnclFtn === 'function')
                cnclFtn();
        }
        setTimeout(function(){$.modal.close();}, 1000);
    });
}

这是我所说的Fiddle link

由于变量没有设置,所以$.modal.close() 不能正常工作,我想要的只是当且仅当按钮OKcancel 被点击时才关闭模式对话框。

【问题讨论】:

  • 哪个变量没有被设置?你的解释太少了。
  • 你可以看到我的编辑flag没有设置@EvanKnowles
  • 您是否尝试过专门将其声明为var flag 而不是使用隐式全局?
  • 是的,我也尝试过,但没有运气,然后我尝试使其全局化,看看它是否有效。 :(

标签: javascript jquery simplemodal


【解决方案1】:

http://www.ericmmartin.com/projects/simplemodal/

来自文档:

onClose:用于为模式对话框元素的关闭添加效果。在你应用了效果等之后,你需要调用 $.modal.close();因此 SimpleModal 可以正确地重新插入数据并清理对话框元素。

您需要在 onClose() 处理程序中包含对 $.modal.close() 的调用,例如

        onClose: function(){
            console.log(flag);
            if(flag) $.modal.close();
        }

ps。至于flag 变量,它的设置完全符合您的预期。这不是问题。

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2014-06-14
    • 2018-07-30
    • 1970-01-01
    • 2014-03-24
    • 2013-11-05
    • 2017-06-21
    • 1970-01-01
    • 2012-03-15
    相关资源
    最近更新 更多