从jquery模型确认或模型对话框返回值

时间:2022-03-22 03:55:18

i have a javascript function like this:

我有这样的javascript函数:

Javascript:

使用Javascript:

function dailog_box(){
    $( "#dialog-confirm" ).dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
            $( this ).dialog( "close" );
            return true;   //i want to return value here
        },
            Cancel: function() {
            $( this ).dialog( "close" );
            return false;  //i want to return value here
            }
        }
    });
}

Html

HTML

<div id="dialog-confirm" title="Prescriptions" style="display:none;">
    <p>
        <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
        These items will be permanently deleted and cannot be recovered. Are you sure?<br/>
        <input type="checkbox" name="check_od" id="check_od"/> The prescription is correct;
    </p>
</div>

After calling the dialog_box() function i want the returned value in the variable flag and i did like this:

在调用dialog_box()函数后,我希望变量标志中返回的值,我喜欢这样:

Javascript

使用Javascript

 var flag = dailog_box();
 alert(flag);

But the result was undefined. and also the alert happens before i click any button. so what should i do to get the value after any button is clicked in the model's button

但结果未定义。并且在我点击任何按钮之前发生警报。那么在模型按钮中单击任何按钮后,我该怎么做才能获得该值

For further information you can check http://jsfiddle.net/8e388/13/ I want the returned value to be alerted. by the way im new to jsfiddle.

有关详细信息,请查看http://jsfiddle.net/8e388/13/我希望提醒返回的值。顺便说一句,我是jsfiddle的新手。

2 个解决方案

#1


1  

I suggest you use callback.

我建议你使用回调。

buttons: {
            Ok: function() {
            callbackSuccess();
            $( this ).dialog( "close" );
        },
            Cancel: function() {
            callbackCancel();
            $( this ).dialog( "close" );
            }
        }

callbackSuccess and callbackCancel - is simple function

callbackSuccess和callbackCancel - 是一个简单的函数

If you create helper for ConfirmDialog it will be easier to pass the callback

如果为ConfirmDialog创建帮助器,则更容易传递回调

#2


2  

You cannot have it exactly as you like. You can do this though using another way :

你无法完全按照自己的意愿拥有它。您可以使用其他方式执行此操作:

Check out my fiddle

看看我的小提琴

Basically I have a global variable feedback

基本上我有一个全局变量反馈

var feedback = false;

And on ok or cancel of the dialog, I set the appropriate value. IMO this adds some more flexibility in your code

在确定或取消对话框时,我设置了适当的值。 IMO为您的代码增加了一些灵活性

#1


1  

I suggest you use callback.

我建议你使用回调。

buttons: {
            Ok: function() {
            callbackSuccess();
            $( this ).dialog( "close" );
        },
            Cancel: function() {
            callbackCancel();
            $( this ).dialog( "close" );
            }
        }

callbackSuccess and callbackCancel - is simple function

callbackSuccess和callbackCancel - 是一个简单的函数

If you create helper for ConfirmDialog it will be easier to pass the callback

如果为ConfirmDialog创建帮助器,则更容易传递回调

#2


2  

You cannot have it exactly as you like. You can do this though using another way :

你无法完全按照自己的意愿拥有它。您可以使用其他方式执行此操作:

Check out my fiddle

看看我的小提琴

Basically I have a global variable feedback

基本上我有一个全局变量反馈

var feedback = false;

And on ok or cancel of the dialog, I set the appropriate value. IMO this adds some more flexibility in your code

在确定或取消对话框时,我设置了适当的值。 IMO为您的代码增加了一些灵活性