Jquery UI对话框作为确认 [英] Jquery UI dialog as confirmation

查看:86
本文介绍了Jquery UI对话框作为确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试建立一个确认对话框,我想把它放在一个函数(在本例中为Globally())因为我在许多不同的函数中使用确认对话框,但这不起作用,控件从 Globally()函数返回获取 true或false 值。我希望它停在那里直到用户按确定或取消。我该怎么做?

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
 }

这是因为只有在确认使用时才能阻止整个javascript执行,并允许你选择一个答案或其他答案(确定,取消)。

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

像jQuery对话框这样的对话框无法阻止脚本执行所以即使你使用

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

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

它只是执行Globally()函数并在它之后继续 - 不等待用户回答。

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

如果你真的想使用jq对话框,那么在你的按钮上添加回调函数。

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

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

在测试函数中删除if / else语句()。

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

这篇关于Jquery UI对话框作为确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆