如何通过更改对话框选项来重用 jQuery UI 对话框

发布于 2024-12-11 22:17:24 字数 419 浏览 0 评论 0原文

我知道管理 JQuery.dialog 的正确方法是初始化:

$("#dialog").dialog({ autoOpen: false });

然后使用以下命令打开和关闭它:

$("#dialog").dialog("open");
$("#dialog").dialog("close");

但在某些情况下此模型并不完全适用。

例如,我使用对话框来创建新数据并编辑现有数据。在第一种情况下,我有一个取消和一个创建按钮,但在第二种情况下,我还有一个删除按钮。

我看到jquery.dialog中有一个销毁函数。问题是:在这些情况下,我应该销毁对话框而不是关闭它并创建一个新对话框吗?还有更好的选择吗?

I know the correct way to manage JQuery.dialog is to initialize with:

$("#dialog").dialog({ autoOpen: false });

Then use the following to open and close it:

$("#dialog").dialog("open");
$("#dialog").dialog("close");

But there are some cases when this model is not fully applicable.

For instance, I use a dialog to create new data and to edit existing data. In the first case I have a cancel and a create button, but in the second case I have also a delete button.

I've seen that there is a destroy function in jquery.dialog. The question is: in these cases, should I destroy the dialog instead of close it and create a new one? There is any better option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆里有你的影子 2024-12-18 22:17:25

jQuery UI 对话框允许您在初始化后操作大多数属性。您可以在对话框初始化后一段时间更改按钮;例如,当单击插入或更新按钮时。

// imported from http://jsfiddle.net/salman/VYAJw/9/

$(function() {
  $("#dialog1").dialog({
    autoOpen: false,
    modal: true
  });
  $("#button-insert").click(function() {
    $("#dialog1").dialog("option", "title", 'Insert Record');
    $("#dialog1").dialog("option", "buttons", [{
      text: "Insert",
      click: function() {
        alert("Record inserted");
        $(this).dialog("close");
      }
    }, {
      text: "Cancel",
      click: function() {
        $(this).dialog("close");
      }
    }]);
    $("#dialog1").dialog("open");
  });
  $("#button-update").click(function() {
    $("#dialog1").dialog("option", "title", 'Update Record');
    $("#dialog1").dialog("option", "buttons", [{
      text: "Update",
      click: function() {
        alert("Record updated");
        $(this).dialog("close");
      }
    }, {
      text: "Delete",
      click: function() {
        alert("Record deleted");
        $(this).dialog("close");
      }
    }, {
      text: "Cancel",
      click: function() {
        $(this).dialog("close");
      }
    }]);
    $("#dialog1").dialog("open");
  });
});
@import url("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/blitzer/jquery-ui.min.css");
body {
  font: medium sans-serif;
}

#dialog1 label,
#dialog1 input {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<div id="dialog1">
  <p>Fill out this form.</p>
  <form>
    <fieldset>
      <label for="name">Name</label>
      <input type="text" name="name" id="name" />
      <label for="email">Email</label>
      <input type="text" name="email" id="email" />
      <label for="password">Password</label>
      <input type="password" name="password" id="password" />
    </fieldset>
  </form>
</div>
<input type="button" id="button-insert" value="Insert" />
<input type="button" id="button-update" value="Update" />

另一种方法是直接在表单内添加按钮,并根据您是否显示插入或更新对话框来 .hide() 它们。

jQuery UI dialog allows you to manipulate most properties after initialization. You can change the buttons some time after the dialog is initialized; e.g. when the insert or update button is clicked.

// imported from http://jsfiddle.net/salman/VYAJw/9/

$(function() {
  $("#dialog1").dialog({
    autoOpen: false,
    modal: true
  });
  $("#button-insert").click(function() {
    $("#dialog1").dialog("option", "title", 'Insert Record');
    $("#dialog1").dialog("option", "buttons", [{
      text: "Insert",
      click: function() {
        alert("Record inserted");
        $(this).dialog("close");
      }
    }, {
      text: "Cancel",
      click: function() {
        $(this).dialog("close");
      }
    }]);
    $("#dialog1").dialog("open");
  });
  $("#button-update").click(function() {
    $("#dialog1").dialog("option", "title", 'Update Record');
    $("#dialog1").dialog("option", "buttons", [{
      text: "Update",
      click: function() {
        alert("Record updated");
        $(this).dialog("close");
      }
    }, {
      text: "Delete",
      click: function() {
        alert("Record deleted");
        $(this).dialog("close");
      }
    }, {
      text: "Cancel",
      click: function() {
        $(this).dialog("close");
      }
    }]);
    $("#dialog1").dialog("open");
  });
});
@import url("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/blitzer/jquery-ui.min.css");
body {
  font: medium sans-serif;
}

#dialog1 label,
#dialog1 input {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<div id="dialog1">
  <p>Fill out this form.</p>
  <form>
    <fieldset>
      <label for="name">Name</label>
      <input type="text" name="name" id="name" />
      <label for="email">Email</label>
      <input type="text" name="email" id="email" />
      <label for="password">Password</label>
      <input type="password" name="password" id="password" />
    </fieldset>
  </form>
</div>
<input type="button" id="button-insert" value="Insert" />
<input type="button" id="button-update" value="Update" />

An alternate method would be to add the buttons directly inside the form and .hide() them depending on whether you're showing insert or update dialog.

今天小雨转甜 2024-12-18 22:17:25

您可以在对话框打开之前设置不同的按钮作为选项,

例如

var  buttons = {
        "act_add": {
        "Insert": function() { ... },
        "Cancel": function() { ... }
        },
        "act_edit": {
        "Save": function() { ... },
        "Delete": function() { ... }
        }
      };

    $('.dialogOpenLink').click(function(){
      var $dlg = $('#dialog'),
      actType;

      //get an action name from data attribute of the clicked element
      actType = $(this).data('action'); //or get the action in way that best suits you

      $dlg.dialog( "option", "buttons", buttons[actType]);
      $dlg.dialog('open');
    });

you can set different buttons as option before dialog open

e.g.

var  buttons = {
        "act_add": {
        "Insert": function() { ... },
        "Cancel": function() { ... }
        },
        "act_edit": {
        "Save": function() { ... },
        "Delete": function() { ... }
        }
      };

    $('.dialogOpenLink').click(function(){
      var $dlg = $('#dialog'),
      actType;

      //get an action name from data attribute of the clicked element
      actType = $(this).data('action'); //or get the action in way that best suits you

      $dlg.dialog( "option", "buttons", buttons[actType]);
      $dlg.dialog('open');
    });
溺渁∝ 2024-12-18 22:17:25

要安全地删除对话框,您需要设置关闭选项,如下所示:

close: function() {
 return $(this).remove();
} 

To safelly remove dialog all you need is to set close option like this:

close: function() {
 return $(this).remove();
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文