【问题标题】:Using Twitter bootstrap modal instead of confirm dialog使用 Twitter 引导模式而不是确认对话框
【发布时间】:2012-08-31 15:31:06
【问题描述】:

我正在尝试使用 twitter 引导模式而不是自定义确认对话框。

我的功能

function getConfirm(confirmMessage){
    if ( confirmMessage == undefined){
        confirmMessage = '';
    }
    $('#confirmbox').modal({
    show:true,
        backdrop:false,
        keyboard: false,
    });

    $('#confirmMessage').html(confirmMessage);
    $('#confirmFalse').click(function(){
        $('#confirmbox').modal('hide');
        return false;
    });
    $('#confirmTrue').click(function(){
        $('#confirmbox').modal('hide');
        return true;
    });
}   
</script>
<div class="modal" id="confirmbox" style="display: none">
    <div class="modal-body">
        <p id="confirmMessage">Any confirmation message?</p>
    </div>
    <div class="modal-footer">
        <button class="btn" id="confirmFalse">Cancel</button>
        <button class="btn btn-primary" id="confirmTrue">OK</button>
    </div>
</div>

调用函数

var confimChange=getConfirm("Do You confirm?");
if(confimChange){
    alert('perfect you confirmed')
}else{
    alert('why didnt you confirmed??')
}

问题是 getConfirm 函数没有返回 true 或 false。


工作代码:

if(receiverListCount > 0){
    var confimChange=confirm("Message");
    if(confimChange){
        resetReceiverList();
    }else{
        return false;
    }
}

修改和更改后的代码片段/不工作

if(activeDiv == '#upload'){
    if(listOfSelectedGroups.length> 0|| receiverListCount > 0){
        getConfirm("message",function(result){
            if(result){
                resetReceiverList();
            }else{
                return false;
            }
        });
    }
}

最后我决定使用 bootboxjs

【问题讨论】:

    标签: jquery twitter-bootstrap modal-dialog confirm


    【解决方案1】:

    你的方法是错误的,它会异步执行,这样它就不会等待模式对话框在返回之前关闭。试试这样的:

    function getConfirm(confirmMessage,callback){
        confirmMessage = confirmMessage || '';
    
        $('#confirmbox').modal({show:true,
                                backdrop:false,
                                keyboard: false,
        });
    
        $('#confirmMessage').html(confirmMessage);
        $('#confirmFalse').click(function(){
            $('#confirmbox').modal('hide');
            if (callback) callback(false);
    
        });
        $('#confirmTrue').click(function(){
            $('#confirmbox').modal('hide');
            if (callback) callback(true);
        });
    }  
    
    getConfirm('Are you sure?',function(result) {
       // Do something with result...
    });
    

    但请注意,您实际上只需要初始化一次模态并挂钩一次事件。我会给出模态正文和 ID,并在显示之前简单地更改内容。

    您还应该将背景设置为 true,您需要确认以防止用户在他们确认之前访问该页面。

    【讨论】:

    • 它运行良好,但我的函数没有等待 getConfirm 函数。
    • 惊人的解决方案。谢谢!
    • 很好的解决方案,但在多次调用后会不一致,因为分配给按钮的回调不会被删除,它们都将在 N 次调用后被评估。如果您没有附加其他事件,则以下内容就足够了:$('#confirmFalse').off().click(function(){
    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 2012-05-13
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2012-02-17
    相关资源
    最近更新 更多