简体   繁体   中英

Return value from jquery model confirm or model dialog box

i have a javascript function like this:

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

<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:

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.

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

I suggest you use callback .

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

callbackSuccess and callbackCancel - is simple function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM