【问题标题】:jQuery popup with Ajax content带有 Ajax 内容的 jQuery 弹出窗口
【发布时间】:2011-07-18 11:45:51
【问题描述】:
【问题讨论】:
标签:
jquery
ruby-on-rails
ajax
jquery-ui
【解决方案1】:
是的,不要认为它是能够使用 json 数据的对话框。这样做:
- 启动你的 ajax
- 在您的处理程序中准备并显示对话框
- 在对话框的按钮处理程序中,如果需要,可以触发更多 ajax,然后处理结果。
关键是要以不同于您在 jQuery UI 网站上看到的示例来考虑对话框。通过浏览 JSON 返回值动态填充对话框,并使用 jQuery 选择器查找您需要的内容,创建更多元素并将新元素插入对话框内容。
这是一个更具体的例子:
$( "#dialog" ).dialog({
modal: true,
buttons: {
Ok: function() {
fire_ok_ajax_with_handler(); //pretend the handler is ok_handler
}
}
});
// this method is called when the action the user takes wants to
// open the dialog. Note that it doesn't actually open the dialog
// but instead starts the ajax process of getting the data it needs
// to prepare the dialog
$( "#opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
fire_ajax_to_start_stuff();
return false;
});
function fire_ajax_to_start_stuff(...) {
// assume start_handler method
}
function start_handler(data) {
//process data, which can be json if your controller formats it that way
// use the data to dynamically setup the dialog,
// show the dialog
$( "#dialog" ).dialog( "open" );
}
function fire_ok_ajax_with_handler() {
// this is where you set up the ajax request for the OK button
}
function ok_handler(data) {
// handle possible errors messages
// close the dialog
$( this ).dialog( "close" );
}
请注意,该示例中有很多伪代码/挥手,但它应该为您提供基本方法。