2013-11-28 214 views
5

我在尝试使用jquery对话框复制javascript的'确认'框。这是我的代码,jquery ui对话框确认

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。

回答

7

你需要做的是使用jQuery.deferred /承诺。

http://api.jquery.com/deferred.promise/

在这个例子中,asyncEvent

1)创建一个jquery推迟对象

2)具有用于解决/拒绝,您的确定逻辑/取消

3)返回一个deferred.promise()对象,然后可以与$ .when一起使用来确定延迟对象是否已解析或拒绝(ok/cancel)。

你会做什么是

1)创建一个jQuery Deferred对象

2)启动您的对话,以确定/取消设定deferred.resolve /拒绝

3)返回一个deferred .promise()对象,

4)使用延迟承诺的对象与$。当 http://api.jquery.com/jQuery.when/

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(); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
       alert(false); 
       dfd.reject(); 
      } 
     } 
    }); 
    return dfd.promise(); 
} 

$.when(customConfirm('hey')).then(
    function() { 
    alert("things are going well"); 
}, 
function() { 
    alert("you fail this time"); 
}); 

你也可以只使用决心,并确定是否确认是在$。当真或假,

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");} 
}); 

希望有所帮助。

+0

在jQuery 3.1.0和jQuery UI 1.12.0尝试创建确认对话框之后,还有其他一些例子。在运行'$ .when()'之前创建一个默认变量。发现在$ .when()完成之前变量已经通过。任何建议或建议? – Twisty

+0

仅供参考 - 我发现我的问题。我正在使用匿名函数,执行后会丢失里面的变量。转到已经定义的功能有所帮助。 – Twisty

2

您应该在文档就绪功能中加载对话框。呼吁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; 
      } 
     } 
    }); 

    }); 
+0

它无法正常工作。仍然显示消息undefined时,我打电话customConfirm –

+0

是'customMessage'参数有什么价值?试着把它整理一下。 – Masudul

+0

我试过这样,alert(customConfirm(“customMessage”)); –

7

这就是我使用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") 
}); 
+0

这很好。但是'var d = new $ .Deferred()'后面缺少分号。它应该是'var d = new $ .Deferred();' – steviethecat

+0

谢谢修复。但JavaScript不关心分号。 – pyros2097