2012-06-13 65 views
1

我希望它在if条件中检查它。我如何使用它?jQuery UI模式没有返回true和false

function notification_dialog_box(title,html,icon) 
{ 
    var text = '<p><span class="'+icon+'" style="float:left; margin:0 7px 50px 0;"></span>'+ html +'</p>'; 
    var $myDialog = $('<div id="dialog-message"></div>').html(text).dialog({ 
     modal: true, zIndex: 10000, autoOpen:true,width: 'auto', modal: true, resizable: false,title: title, 
     buttons: { 
      "OK":function(){$(this).dialog("close"); return true;}, 
      "Cancel":function() {$(this).dialog("close");return false;} 
      } 
     }); 
} 

if(notification_dialog_box('Out of designer\'s', 'All Designer\'s are assigned in this project', 'ui-icon ui-icon-info')) 
{ 
code.... 
} 

回答

4

你正在返回true/false在asyc回调。

:返回true代码/ false并不运行,直到用户点击一个按钮,但你的代码已经运行完毕。

您需要切换到回调结构以使您的代码正常工作。

ie:您需要将函数传递给notification_dialog_box,当用户单击某个按钮并传递它们所点击的值时会调用该函数。

是这样的:

function notification_dialog_box(title,html,icon, fn) 
{ 
    var text = '<p><span class="'+icon+'" style="float:left; margin:0 7px 50px 0;"></span>'+ html +'</p>'; 
    var $myDialog = $('<div id="dialog-message"></div>').html(text).dialog({ 
     modal: true, zIndex: 10000, autoOpen:true,width: 'auto', modal: true, resizable: false,title: title, 
     buttons: { 
      "OK":function(){$(this).dialog("close"); fn(true);}, 
      "Cancel":function() {$(this).dialog("close"); fn(false);} 
      } 
     }); 
} 

notification_dialog_box('title', 'blah blah blah', 'ui-icon ui-icon-info', function(ok){ 
    if(ok) { 
    //code 
    } 
}) 
1

您不能使用jQuery的模型箱为JavaScript的Confirm()即JavaScript不等待用户响应

正确的方法是使用回调函数。

function notification_dialog_box(title, html, icon, success, failure) { 
    var text = '<p><span class="' + icon + '" style="float:left; margin:0 7px 50px 0;"></span>' + html + '</p>'; 
    var $myDialog = $('<div id="dialog-message"></div>').html(text).dialog({ 
     modal: true, 
     zIndex: 10000, 
     autoOpen: true, 
     width: 'auto', 
     modal: true, 
     resizable: false, 
     title: title, 
     buttons: { 
      "OK": function() { 
       $(this).dialog("close"); 
       success() 
      }, 
      "Cancel": function() { 
       $(this).dialog("close"); 
       failure() 
      } 
     } 
    }); 
} 

if (notification_dialog_box('Out of designer\'s', 'All Designer\'s are assigned in this project', 'ui-icon ui-icon-info', function() { 
    // success code here 
}, function() { 
    // error code here 
}) ​ 
1

你可以做这样的事情

正如@mkoryak说你不能返回这样的,从一个异步回调。相反,你可以做类似

function notification_dialog_box(title,html,icon,callback) 
{ 
    var text = '<p><span class="'+icon+'" style="float:left; margin:0 7px 50px 0;"></span>'+ html +'</p>'; 
    var $myDialog = $('<div id="dialog-message"></div>').html(text).dialog({ 
     modal: true, zIndex: 10000, autoOpen:true,width: 'auto', modal: true, resizable: false,title: title, 
     buttons: { 
      "OK":function(){$(this).dialog("close"); callback(true);}, 
      "Cancel":function() {$(this).dialog("close");callback(false);} 
      } 
     }); 
} 



function dialog_callback(retVal){ 
    if(retVal) 
     // user clicked on Ok 
    else 
     // clicked on Cancel 
} 

notification_dialog_box('Out of designer\'s', 
    'All Designer\'s are assigned in this project', 
    'ui-icon ui-icon-info', 
     dialog_callback // pass the callback 
);