2010-10-04 111 views
1

我有一个应用程序,有很多对话框,并创建一个函数来打开一个对话框,并加载数据到其中,所以其他功能可以打开一个对话框并处理用户选项。jquery多个对话框将值返回给函数调用它

问题是当我打电话给openDialog时,它会停止调用它的函数。我想通过添加一个返回值,所以当一个按钮被点击时,调用函数可以处理用户的响应。

function customer_crud(op) 
{ 
var formData = $("#customer_details_form").serialize(); 
var debugData = formData.replace(/&/g,'<br />'); 

var text = "<p>Customer Function: " + op + "</p><p>" + debugData + "</p>"; 
if(openDialog('DEBUG', text)){ 
    alert("TRUE"); 
} else { 
    alert("FALSE"); 
} 

} 

function openDialog(title, text) { 

    var dialogOpts = { 
    title: title, 
    modal: true, 
    autoOpen: false, 
    buttons: { 
       "Delete all items": function() { 
       $(this).dialog("close"); 
       return true; 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
       return false 
      } 
    } 

    }; 

    $("#dialog").dialog(dialogOpts); 

    $("#dialog").html(text).dialog('open'); 

}

上面的代码打开的对话框中,但选择什么之前抛出假的。如果有人能够帮助我或建议一个更好的方式,我会很棒。

我打算将dialogOpts传递给函数,但将它放在那里进行测试。

谢谢

回答

0

我有一个可以从多个按钮调用的单个对话框。只有一个步骤会根据按下哪个按钮而发生变化。根据按钮,对话框的结果将进入不同的字段。所以,我在调用对话框之前在click函数中设置了一个变量。然后,在我的对话框中,我有一个switch语句,它检查变量并将该值添加到适当的字段。您可以同样使用switch语句来完成不同的功能,具体取决于您所调用的对话框。

function openDialog(title, text) { 
    var dialogOpts = { 
    title: title, 
    modal: true, 
    autoOpen: false, 
    buttons: { 
     "Delete all items": function() { 
     switch (item_type) { 
            case "primary": 
             ... 
             break; 
            case "insurance": 
             ... 
             break; 
            case "safety": 
             ... 
             break; 
            case "sales": 
             ... 
             break; 
           } 
           $(this).dialog('close'); 
     }, 
     Cancel: function() { 
     $(this).dialog("close"); 
     } 
    } 
    }; 
    $("#dialog").dialog(dialogOpts) 
      .html(text).dialog('open'); 
} 
0

你的openDialog函数没有返回值。这就是为什么你总是会得到假。

0

你真的不能有这样的对话,因为它不是同步/阻塞,更好的方法是调用所需的方法,当你点击对话框按钮,如下所示:

function customer_crud(op) 
{ 
    var formData = $("#customer_details_form").serialize(); 
    var debugData = formData.replace(/&/g,'<br />'); 

    var text = "<p>Customer Function: " + op + "</p><p>" + debugData + "</p>"; 
    openDialog('DEBUG', text); 
} 

function delete_items() { 
    alert("Deleting!"); 
} 

function openDialog(title, text) { 
    var dialogOpts = { 
    title: title, 
    modal: true, 
    autoOpen: false, 
    buttons: { 
     "Delete all items": function() { 
     $(this).dialog("close"); 
     delete_items(); 
     }, 
     Cancel: function() { 
     $(this).dialog("close"); 
     } 
    } 
    }; 
    $("#dialog").dialog(dialogOpts) 
      .html(text).dialog('open'); 
}