jQueryUI对话框替换确认? [英] jQueryUI dialog replacement for confirm?

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

问题描述

我试图用jQueryUI对话框覆盖默认确认框,这是我的尝试:

I am trying to override the default confirmation box with the jQueryUI dialog box, and here is my attempt:

window.confirm = function (message_confirm) {
        $(document.createElement('div'))
            .attr({ title: 'Please confirm', 'class': 'confirm', 'id': 'dialogconfirm' })
            .html(message_confirm)
            .dialog({
                buttons: { YES: function () { return true; }, NO: function () { $(this).dialog('close'); } },
                close: function () { $(this).remove(); },
                draggable: true,
                modal: true,
                resizable: false,
                width: 'auto',
                hide: { effect: "fade", duration: 300 },

            });

    };

一旦运行此脚本,调用常规确认显示jQueryUI对话框,但 YES 选择不起作用。我有一个像下面这样的ajax调用:

This works in that once this script is run, calling a regular confirm shows the jQueryUI dialog, however, the YES selection does not work. I have an ajax call like below:

if (confirm("Are you sure you want to delete this user?"))
{
      alert("test");
     //ajax call
}
return false;

并且没有出现测试警报。对话框的NO选择工作正常。如何让YES工作?

And the test alert did not appear. The NO slection of the dialog works fine. How can I get the YES to work?

谢谢。

推荐答案

因为对话框是异步的,你可以使用延迟方法:

Because the dialog is asynchronous you can use a deferred approach:

window.confirm = function (message_confirm) {
  var defer = $.Deferred();
  $('<div/>', {title: 'Please confirm', 'class': 'confirm', 'id': 'dialogconfirm', text: message_confirm})
  .dialog({
    buttons: {
      YES: function () {
        defer.resolve("yes");
        $(this).attr('yesno', true);
        $(this).dialog('close');
      }, NO: function () {
        defer.resolve("no");
        $(this).attr('yesno', false);
        $(this).dialog('close');
      }
    },
    close: function () {
      if ($(this).attr('yesno') === undefined) {
          defer.resolve("no");
      }
      $(this).remove();
    },
    draggable: true,
    modal: true,
    resizable: false,
    width: 'auto',
    hide: {effect: "fade", duration: 300}
  });
  return defer.promise();
};

$(function () {
  confirm("Are you sure you want to delete this user?").then(function(yesno) {
    console.log(yesno);
  });
});

<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

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

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