Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery ui对话框作为确认对话框_Javascript_Jquery_Jquery Ui Dialog - Fatal编程技术网

Javascript jquery ui对话框作为确认对话框

Javascript jquery ui对话框作为确认对话框,javascript,jquery,jquery-ui-dialog,Javascript,Jquery,Jquery Ui Dialog,我正在尝试使用jquery对话框复制javascript的“确认”框。这是我的密码 function customConfirm(customMessage) { $("#popUp").html(customMessage); $("#popUp").dialog({ resizable: false, height: 240, modal: true, button

我正在尝试使用jquery对话框复制javascript的“确认”框。这是我的密码

function customConfirm(customMessage) {
        $("#popUp").html(customMessage);
        $("#popUp").dialog({
            resizable: false,
            height: 240,
            modal: true,
            buttons: {
                "OK": function () {
                    $(this).dialog("close");
                    alert(true);
                    return true;
                },
                Cancel: function () {
                    $(this).dialog("close");
                    alert(false);
                    return false;
                }
            }
        });
    }
但当我试图警告这个方法时,它显示“未定义”。它不是等待弹出窗口显示。如何使此customConfirm功能等待用户输入(确定/取消)?。
我需要的是,customConfirm()方法将根据用户输入返回true或false。

您应该在document ready函数上加载对话框。调用对话框打开
customConfirm
功能

  function customConfirm(customMessage) {
    $("#popUp").html(customMessage);
    $("#popUp").dialog("open");
  }

  $(document).ready(function (){
    $("#popUp").dialog({
        resizable: false,
        autoOpen: false,
        height: 240,
        modal: true,
        buttons: {
            "OK": function () {
                $(this).dialog("close");
                alert(true);
                return true;
            },
            Cancel: function () {
                $(this).dialog("close");
                alert(false);
                return false;
            }
        }
    });

  });

您需要做的是使用jQuery.deferred/promise

在本例中,asyncEvent

1) 创建jquery延迟对象

2) 具有解决/拒绝、确定/取消的逻辑

3) 返回一个deferred.promise()对象,然后该对象可以与$一起使用。确定何时解析或拒绝延迟对象(确定/取消)

你要做的是

1) 创建jquery延迟对象

2) 启动对话框,确定/取消设置为deferred.resolve/reject

3) 返回延迟的.promise()对象

4) 将延迟承诺对象与$.when一起使用

您也可以使用resolve来确定$中的confirm是true还是false

function customConfirm(customMessage) {
    var dfd = new jQuery.Deferred();
    $("#popUp").html(customMessage);
    $("#popUp").dialog({
        resizable: false,
        height: 240,
        modal: true,
        buttons: {
            "OK": function () {
                $(this).dialog("close");
                alert(true);
                dfd.resolve(true);
            },
            Cancel: function () {
                $(this).dialog("close");
                alert(false);
                dfd.resolve(false);
            }
        }
    });
   return dfd.promise();
}

$.when( customConfirm('hey') ).then(
  function(confirm) {

   if(confirm){alert( "things are going well" );}
   else{alert( "you fail this time" );}
});

希望有帮助

这就是我使用zepto和模块延迟和回调所做的,工作起来很有魅力。 jquery应该类似,或者您可以将延迟和回调模块导入html

function customConfirm(customMessage) {
  var d = new $.Deferred();
  $("#popUp").html(customMessage);
  $("#popUp").dialog({
      resizable: false,
      height: 300,
      modal: true,
      buttons: {
          "Yes": function () {
              $(this).dialog("close");
              d.resolve()
          },
          "No": function () {
              $(this).dialog("close");
              d.reject();
          }
      }
  });
 return d.promise();
}

customConfirm("Do you Want to delete the File?")
.then(function(){
  console.log("You Clicked Yes")
})
.fail(function(){
  console.log("You Clicked No")
});

它不起作用。当我调用customConfirmIs
customMessage
param有任何值时,它仍然显示未定义的消息?尝试将其consol.log。@AnoopJoshi,警报(customConfirm)对显示对话框无效。尝试`$(“#确认”).button()。单击(函数(){$(“#弹出”).html(“你好”);$(“#弹出”).dialog(“打开”);})`它将在内部调用dialog.open方法时明确显示弹出对话框。我的疑问是,是否有可能在javascript中复制确认框。这非常有效。但是,在
var d=new$.Deferred()之后缺少分号。它应该是
var d=new$.Deferred()谢谢修复。但是javascript不关心分号。过去对我来说。你好吗?什么?投资比特币,致富,拯救世界。以下是jQuery 3.1.0和jQuery UI 1.12.0尝试创建确认对话框的其他示例。在运行
$之前正在创建默认变量。when()
。发现变量在
$之前通过。当()
完成时。有什么建议吗?仅供参考-我发现了我的问题。我使用的是匿名函数,其中的变量在执行后会丢失。移动到已定义的函数会有所帮助。
function customConfirm(customMessage) {
  var d = new $.Deferred();
  $("#popUp").html(customMessage);
  $("#popUp").dialog({
      resizable: false,
      height: 300,
      modal: true,
      buttons: {
          "Yes": function () {
              $(this).dialog("close");
              d.resolve()
          },
          "No": function () {
              $(this).dialog("close");
              d.reject();
          }
      }
  });
 return d.promise();
}

customConfirm("Do you Want to delete the File?")
.then(function(){
  console.log("You Clicked Yes")
})
.fail(function(){
  console.log("You Clicked No")
});