我需要用 bootbox.confirm 替换确认框

问题描述 投票:0回答:1

考虑:

var isSaveRequired = false;
    
     function saveChanges() {
        if(isSaveRequired ) {
            if(confirm("<%=strings.getText("This is come from server side")%>")) {
                isSaveRequired=false;
                return true;
            }
            else {
                return false;
            }
        }
        return true;
    }

这里我需要将

confirm
替换为
bootbox.confirm
(在我们的项目中,bootbox.js库已经存在)。

function saveChanges() {
    if (isSaveRequired) {
        bootbox.confirm('<%= strings.getText("this msg come from server side") %>', function(result) {
            if (result) {
                isSaveRequired = false;
                return true;
                
            }
        });
        return false; 
    }
    return true; 
}

但这里的问题是弹出窗口(未保存的数据将丢失)仅显示正确,但如果我单击确定,那么它不会重定向到基于用户选择的另一个页面。

但这里的问题是弹出窗口(未保存的数据将丢失)仅显示正确,但如果我单击确定,那么它需要导航到基于用户选择的另一个页面。

javascript jquery bootbox
1个回答
0
投票

我们在文档中的很多地方都注意到了这一点:Bootstrap 模态(因此 Bootbox 模态)是 异步

所有编程 API 方法都是异步的,并在转换开始后但在结束之前返回到调用者。

所以在这种情况下:

function saveChanges() {
    if (isSaveRequired) {
        bootbox.confirm('<%= strings.getText("this msg come from server side") %>', function(result) {
            if (result) {
                isSaveRequired = false;
                return true;
                
            }
        });
        return false; 
    }
    return true; 
}

saveChanges()
将在对话框仍处于活动状态时完成。因此,如果您希望仅在用户确认某些内容时才发生重定向之类的操作,那么您可能需要类似的操作:

function saveChanges() {
    if (isSaveRequired) {
        bootbox.confirm('<%= strings.getText("this msg come from server side") %>', function(result) {
            if (result) {
                // not sure where this comes from or how it's used, 
                // so leaving it here
                isSaveRequired = false;

                // perform a JS redirect - location.href is one option, or 
                // use something from: 
                // https://developer.mozilla.org/en-US/docs/Web/API/Window/location
                window.location.href = 'your redirect URL here';
            }
        });
        
        // assuming this is triggered by a button or anchor element,
        // 'return false' short-circuits the normal action
        return false; 
    }

    return true; 
}

披露:我是 Bootbox 项目的开发人员之一

© www.soinside.com 2019 - 2024. All rights reserved.