18

可能重复:
是或否使用 jQuery 的确认框

我在这里看到很多关于替换 jQuerys 标准确认对话框的示例,但不理解其中任何一个。

如果我想使用 jQuery 或 jQueryUI 弹出一个确认对话框。我该怎么做并检索用户单击的按钮?

当然,必须有一种简单的方法来设置像alert("Something!");命令一样的确认对话框。

向其添加两个按钮并设置回调函数 on yesor no?

4

3 回答 3

51

试试这个

$('<div></div>').appendTo('body')
  .html('<div><h6>Yes or No?</h6></div>')
  .dialog({
      modal: true, title: 'message', zIndex: 10000, autoOpen: true,
      width: 'auto', resizable: false,
      buttons: {
          Yes: function () {
              doFunctionForYes();
              $(this).dialog("close");
          },
          No: function () {
              doFunctionForNo();
              $(this).dialog("close");
          }
      },
      close: function (event, ui) {
          $(this).remove();
      }
});

小提琴

于 2012-09-27T08:38:28.823 回答
13

您可以使用 jQuery UI 并执行类似的操作

html:

<button id="callConfirm">Confirm!</button>

<div id="dialog" title="Confirmation Required">
  Are you sure about this?
</div>​

Javascript:

$("#dialog").dialog({
   autoOpen: false,
   modal: true,
   buttons : {
        "Confirm" : function() {
            alert("You have confirmed!");            
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

$("#callConfirm").on("click", function(e) {
    e.preventDefault();
    $("#dialog").dialog("open");
});

​</p>

于 2012-09-27T08:37:12.713 回答
5

您是否尝试过使用官方的 JQueryUI 实现(不仅仅是 jQuery) :?

于 2012-09-27T08:39:04.930 回答