【问题标题】:Why won't my jQuery-ui modal dialog display two custom buttons?为什么我的 jQuery-ui 模态对话框不显示两个自定义按钮?
【发布时间】:2013-07-27 06:24:36
【问题描述】:

我有一个通用的 Javascript 函数,用于显示带有两个按钮的 jQuery-ui 模式对话框——本质上是“继续”和“取消”,尽管文本有所不同。我在我的应用程序的三个地方调用它。发生的事情是只显示第二个按钮,即“取消”按钮。这是函数:(String.Format 是我一直使用的一个外部函数,因为 Javascript 没有内置函数 - 我知道这不是问题。)

function DisplayModalDialog(titleText, bodyText, continueText, cancelText) {
  //add the dialog div to the page
  $('body').append(String.Format("<div id='theDialog' title='{0}'><p>{1}</p></div>", titleText, bodyText));
  //create the dialog
  $('#theDialog').dialog({
    width: 400,
    height: "auto",
    modal: true,
    resizable: false,
    draggable: false,
    close: function (event, ui) {
      $('body').find('#theDialog').remove();
      $('body').find('#theDialog').destroy();
    },
    buttons: [
    {
      text: continueText,
      click: function () {
        $(this).dialog('close');
        return true;
      },
      text: cancelText,
      click: function () {
        $(this).dialog('close');
        return false;
      }
    }]
  });

  return false;
}

这里有一个 sn-p 显示我是如何称呼它的:

if(CheckFormDataChanged() {
  var changeTitle = "Data has been changed";
  var changeText = "You have updated information on this form. Are you sure you wish to continue without saving?";
  var changeContinue = "Yes, continue without saving";
  var changeCancel = "No, let me save";
  if (DisplayModalDialog(changeTitle, changeText, changeContinue, changeCancel)) {
    if (obj) obj.click();
    return true;
  }
}

我的函数(或调用)有什么问题?

更新:这是我现在正在使用的。我意识到在其中一个模式对话框中我不需要取消按钮,只需要一个确认按钮:

function DisplayModalDialog(titleText, bodyText, continueText, cancelText, suppressCancel) {
  var def = new $.Deferred();
  //add the dialog div to the page
  $('body').append(String.Format("<div id='theDialog' title='{0}'><p>{1}</p></div>", titleText, bodyText));
  //create the button array for the dialog
  var buttonArray = [];
  buttonArray.push({ text: continueText, click: function () { $(this).dialog('close'); def.resolve(); } });
  if (!suppressCancel) {
    buttonArray.push({ text: cancelText, click: function () { $(this).dialog('close'); def.reject(); } });
  }
  //create the dialog
  $('#theDialog').dialog({

    ... dialog options ...

    close: function (event, ui) { $('body').find('#theDialog').remove(); },
    buttons: buttonArray
  });
  return def.promise();
}

及用法:

DisplayModalDialog(changeTitle, changeText, changeContinue, changeCancel, false)
  .done(function () { if (obj) obj.click(); return true; })
  .fail(function () { return false; });

只是为了给你一些上下文,obj 是一个传递给客户端函数的 ASP.Net 按钮;如果函数返回true,则触发服务器端的OnClick事件;如果为假,则不是。在这种情况下,服务器端 OnClick 前进到 TabContainer 中的下一个选项卡(除其他外)。发生的事情是它无论如何都会移动到下一个选项卡,即使我在 fail() 函数中返回 false。

【问题讨论】:

  • 您的函数将不起作用,因为 jQuery 对话框不会阻止执行。对话行被执行,紧随其后的是return false;。 Alnitak 的例子正是我在这种情况下所做的。
  • 我已经更新了 OP 并提供了更多关于为什么 return false; 应该 在这种情况下工作的上下文和信息。很抱歉之前没有提供此信息。
  • 附注您应该在 close: 处理程序中调用 $(this).dialog('destroy').remove()

标签: javascript jquery jquery-ui jquery-ui-dialog


【解决方案1】:

您的花括号已关闭:

[{
    text: continueText,
    click: function () {
        $(this).dialog('close');
        return true;
    }
}, {
    text: cancelText,
    click: function () {
        $(this).dialog('close');
        return false;
    }
}]

正如你所拥有的,你的按钮数组中只有一个对象。

【讨论】:

  • 是的,我在等待 cmets 时发现了花括号错误 - 谢谢!现在我需要尝试理解 Alnitak 上面所说的内容,以便让对话框真正正常工作。
【解决方案2】:

我还不知道为什么按钮不显示EDIT,啊,是的,我可以,缺少一个大括号。

可以告诉你,你的 return 行根本行不通。

显示对话框,您的函数立即返回,继续处理,因此click 回调返回值被完全忽略。

你可以做的是返回一个承诺:

function DisplayModalDialog(titleText, bodyText, continueText, cancelText) {
    var def = $.Deferred();
    ...
    buttons: [
    {
        text: continueText,
        click: function () {
            $(this).dialog('close');
            def.resolve();
        }
    },   
    {    // ah - here's your button bug - a missing brace
        text: cancelText,
        click: function () {
            $(this).dialog('close');
            def.reject();
        }
    }

    ...

    return def.promise();
}

有用法:

DisplayModalDialog(changeTitle, changeText, changeContinue, changeCancel)
   .done(function() {
       // continue was clicked
   }).fail(function() {
       // cancel was clicked
   });

【讨论】:

  • 你的用法不太对。您没有在 deferred 上调用 reject(),因此不会执行 fail()。用法应该类似于.done(function(result) { if (result) { /* continue */ } else { /* cancel */ } });
  • @JasonP 哎呀,是的,你是对的。我的错 - 编码太晚了 :) 我已将其更改为使用 .reject 而不是 .resolve(false)
  • @timbck2 我已经修复了我的示例,希望它现在应该有意义了。
  • 不用担心。我会留下我的评论作为替代方案。我更喜欢resolve(false),但我确信这是个人喜好问题。
  • @JasonP 如果有多个“接受”选项,我倾向于使用resolve(foo),但reject 用于“取消”操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
相关资源
最近更新 更多