简体   繁体   English

jQuery UI对话框作为确认

[英]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. 我正在尝试创建一个确认对话框,并且希望将其放置在一个函数中(在本例中为Globally()),因为我在许多不同的函数中使用了确认对话框,但这无法正常工作,控件从Globally()函数,而不会获得true or false值。 I want it to stop there until user press Ok or Cancel . 我希望它停在那里,直到用户按“ 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). 这是因为仅在使用时进行确认会阻止整个javascript执行,并允许您选择一个答案或另一个答案(确定,取消)。

Dialogs like jQuery dialog can not stop script execution so even if you use 像jQuery对话框之类的对话框无法停止脚本执行,因此即使您使用

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

It'll just execute Globally() function and continue right after it - not waiting for a user answer. 它只会执行Globally()函数并紧接着继续执行-无需等待用户回答。

If you really want to use jq dialog then add callback functions to your buttons. 如果您确实要使用jq对话框,则将回调函数添加到按钮中。

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

And ditch if/else statement() in test function. 然后在测试函数中放弃if / else statement()。

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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