Javascript jquery UI 对话框的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8740021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:17:34  来源:igfitidea点击:

Return value of jquery UI dialog Box

javascriptjqueryjquery-uijquery-ui-dialog

提问by Umesh Patil

You may find solution over this in many posts(Post 1, Post2), but their solution not working for me.

您可能会在许多帖子(Post 1Post2)中找到解决方案,但他们的解决方案对我不起作用。

Here is the normal jquery dialog box written by me.

下面是我写的普通jquery对话框。

$("#dialog").dialog({
        autoOpen:false,
        buttons:{
            "ok":function(){                                        
                        $(this).dialog("close"); 
                        return true;                                                                    
                    },
            "cancel":function(){                          
                        $(this).dialog("close");     return false;                  
                }
            }   
});

I will open the dialogbox with code:

我将用代码打开对话框:

var returnVal=$("#dialog").dialog("open");

I need to return false,if user clicks 'cancel' and return trueif user clicks 'ok'.

我需要返回false,如果用户点击“取消”并返回,true如果用户点击“确定”。

var returnVal=$("#dialog").dialog("open");

I NEED returnValto return booleanvalue(true/false), but it returns javascript object.

我需要returnVal返回boolean值(真/假),但它返回 javascript object

回答by Manse

You cannot return something from the OK / cancel functions as they are essentially event handlers that are only processed upon the click of a button.

您不能从 OK / cancel 函数返回某些内容,因为它们本质上是仅在单击按钮时处理的事件处理程序。

Use a separate function to process the result :

使用单独的函数来处理结果:

$mydialog = $("#dialog").dialog({
    autoOpen: false,
    buttons: {
        "ok": function() {
            $(this).dialog("close");
            processResult(true);
        },
        "cancel": function() {
            $(this).dialog("close");
            processResult(false);
        }
    }
});

$mydialog.dialog("open");


function processResult(result) {
    alert(result);
}

Working example : http://jsfiddle.net/nz2dH/

工作示例:http: //jsfiddle.net/nz2dH/

回答by Ilkka

I have implement Yes/No confirmation dialog with custom message and callback function like this. This is useful, if you like to use the same dialog for various purposes.

我已经使用这样的自定义消息和回调函数实现了是/否确认对话框。如果您喜欢将同一个对话框用于各种目的,这很有用。

<script type="text/javascript">
    // prepare dialog
    $(function () {
        $("#confirm-message-dialog").dialog({
            autoOpen: false,
            modal: true,
            closeOnEscape: false,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $(this).data("callback")(true);
                },
                No: function () {
                    $(this).dialog("close");
                    $(this).data("callback")(false);
                }
            }
        });
    });

    // open dialog with message and callback function
    function confirmMessageDialog (message, callback) {
        $('#confirm-message-dialog-message').text(message);
        $('#confirm-message-dialog').data("callback", callback).dialog("open");
    };
</script>

<!-- The dialog content -->
<div id="confirm-message-dialog" title="Warning"> 
    <p id="confirm-message-dialog-message"></p>
</div>

Hope that this helps others as well :)

希望这对其他人也有帮助:)