简体   繁体   中英

Jquery UI dialog as confirmation

  function Globally() {
    $("#dialogid").dialog({ width: 300, modal: true, show: 'drop', hide: 'drop',
        buttons: {
            "Ok": function () { return true; $(this).dialog('close'); },
            "Cancel": function () { return false; $(this).dialog('close'); }
        }
    });
  }

  function test()
  {
    if(Globally())
      alert("Ok");
    else
      alert("Cancel");
  }

I am trying to make a confirmation dialog and I want it to placed in a function (in this case Globally()) because I am using confirmation dialog in so many different function, but this is not working , the control returns from the Globally() function without getting true or false value. I want it to stop there until user press Ok or Cancel . How can I do this?

You'll have to use built in confirm function if you want to run code like that:

var question = confirm("Proceed?")
if (question){
   // continue
 } else {
   // stop
 }

That is because only confirm when used prevent whole javascript execution and allows you to pick one answer or the other (Ok, Cancel).

Dialogs like jQuery dialog can not stop script execution so even if you use

if(Globally())
  alert("Ok");
else
  alert("Cancel");

It'll just execute Globally() function and continue right after it - not waiting for a user answer.

If you really want to use jq dialog then add callback functions to your buttons.

"Ok": function () { callbackFunctionTrue(); },
"Cancel": function () { callbackFunctionFalse(); }

And ditch if/else statement() in test function.

That;s not how it works

Just do

    "Ok": function () { alert('OK'); $(this).dialog('close'); },
    "Cancel": function () { alert('Not ok'); $(this).dialog('close'); }

or

    "Ok": function () { $(this).dialog('close'); test(1) },
    "Cancel": function () {$(this).dialog('close'); test(0) }

with

function test(ok){
  alert(ok?"Ok":"Cancel");
}

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