jQuery同步确认对话框

jQuery同步确认对话框,jquery,twitter-bootstrap,event-handling,confirm,Jquery,Twitter Bootstrap,Event Handling,Confirm,我使用jQuery+Bootstrap+jqGrid插件来处理网格。 在网格中进入编辑模式时,可以告诉该插件退出编辑模式(并保存更改)或保持编辑模式(不保存)。 现在,该编辑模式只能通过“确认”对话框关闭。 不幸的是,这似乎很难实现,因为事件是异步的 简单示例(未经确认): 以下情况不起作用: //this won't not work: true will always be returned! var saveRow = true; //... $grid.on("beforesaverow

我使用jQuery+Bootstrap+jqGrid插件来处理网格。 在网格中进入编辑模式时,可以告诉该插件退出编辑模式(并保存更改)或保持编辑模式(不保存)。 现在,该编辑模式只能通过“确认”对话框关闭。 不幸的是,这似乎很难实现,因为事件是异步的

简单示例(未经确认):

以下情况不起作用:

//this won't not work: true will always be returned!
var saveRow = true;
//...
$grid.on("beforesaverow", function(){
    confirmDialog("Save row?", function(answer){ //...open a simple bootstrap dialog with question yes/no...
        //...entering callback...
        if (answer){
            saveRow = true;
        }
        else{
            saveRow = false;
        }
    });
    return saveRow; //NOT OK: saveRow will always be "true"... function is ended but confirm is still busy...
});
现在该如何实施:

var saveRow = true;
//...
$grid.on("beforesaverow", function(){
    confirmDialog("Save row?", function(answer){ //...open a simple bootstrap dialog with question yes/no...
        //...entering callback...
        if (answer){
            saveRow = true;
            //obviously this is not correct because we are NOT returning "true" from "beforesaverow"...
            return true; //alternative needed...
        }
        else{
            saveRow = false;
        //obviously this is not correct because we are NOT returning "false" from "beforesaverow"...
        return false; //alternative needed...
        }
    });
});
这只是一个例子。。。我认为这个问题可以发生在任何地方使用这种结构

使用简单的旧javascript可以工作,但编码不好:

$grid.on("beforesaverow", function(){
    var answer = window.confirm("Save row?"); //...open the browser's dialog with question ok/cancel...
    if (answer){
        saveRow = true;
    }
    else{
        saveRow = false;
    }
    return saveRow; //perfect! either true or false will be returned!
});
浏览器的确认对话框就像一个警报:程序流将暂停,直到用户关闭对话框。。。但这是不好的练习

@stackoverflow我已经找到了一些关于延迟对象的东西,但这也不起作用。 问题似乎是子函数应该返回它的父函数,我猜

另一个澄清jqGrid并非真正的问题的示例:

$elem.on("click", function(e){
    var stopEventBubbling = false;
    confirmDialog("cancel this click event?", function(answer){
        stopEventBubbling = answer;
    }
    return stopEventBubbling; // the value of "stopEventBubbling" will never be the returned "answer" form the confirm dialog!
});

使用哪个版本的jqGrid?你如何调用编辑功能?嗨,托尼,4.6.0版。但jqGrid并不是真正的问题。。。我在帖子里的代码只是某种伪代码。i、 e.您可以将beforesaverow事件替换为click事件:问题仍然存在……使用了哪个版本的jqGrid?你如何调用编辑功能?嗨,托尼,4.6.0版。但jqGrid并不是真正的问题。。。我在帖子里的代码只是某种伪代码。i、 e.您可以将beforesaverow事件替换为click事件:问题仍然存在。。。
$elem.on("click", function(e){
    var stopEventBubbling = false;
    confirmDialog("cancel this click event?", function(answer){
        stopEventBubbling = answer;
    }
    return stopEventBubbling; // the value of "stopEventBubbling" will never be the returned "answer" form the confirm dialog!
});