jQuery UI 对话框 - 更改打开的对话框的内容 (Ajax)
我想使用 jQuery.load()
在 jQuery UI 对话框中动态打开一些链接。对话框打开后,我希望链接加载到已打开的对话框中。
- 因此,该网站加载后,您单击一个链接,它会在一个对话框中打开。没关系。您可以根据需要多次关闭和打开它。
- 当它打开时,如果您单击加载内容中的链接之一,它将不起作用。
链接如下所示...
<a href="http://www.example.com/index.php?action=something&search=somethingelse#fragment" rel="dialog" title="Title Attribute">
单击事件已绑定...
$('body').delegate("a[rel~=dialog]", "click", function(e){return ajax_dialog(this, e);});
ajax_dialog
函数检查是否存在对话框,如果存在则调用创建一个对话框没有,调用加载内容,设置标题,并打开对话框(如果未打开)。
function ajax_dialog(_this, _event){
var urlToLoad = $(_this).attr("href").replace("#", "&ajax=true #");
var linkTitle = $(_this).attr("title");
// Create dialog
if(!$('body').find('#ajaxDialog').size()){
$('body').append('not yet init<br />'); // This shows up the first click only.
init_dialog('#ajaxDialog');
}
// Load Dialog Content
load_dialog('#ajaxDialog', urlToLoad);
// Add title
$('#ajaxDialog').dialog('option', 'title', linkTitle);
// Open dialog (or reload)
if(!$('#ajaxDialog').dialog('isOpen')){
$('#ajaxDialog').dialog('open');
$('body').append('not yet open<br />'); // This shows up the first click only.
}
return false;
}
如果没有对话框,init_dialog
函数将创建对话框...
function init_dialog(_this){
$('body').append('<div id="ajaxDialog"></div>');
// Set Dialog Options
$(_this).dialog({
modal:true,
autoOpen:false,
width:900,
height:400,
position:['center','center'],
zIndex: 9999,
//open:function(){load_dialog(this, urlToLoad);}, This didn't work without destroying the dialog for each click.
close:function(){$(this).empty();}
});
}
load_dialog
函数将所需的内容加载到对话框中。
function load_dialog(_this, urlToLoad){
$(_this).load(urlToLoad, function(){
$('body').append(urlToLoad + ' load function<br />'); // This shows up each click
$(_this).append("Hihi?"); // This shows up each click
});
// The loaded information only shows the first click, other times show an empty dialog.
}
I've got some links I want to have dynamically open in a jQuery UI Dialog using jQuery.load()
. Once the dialog is open, I want the links load inside the already opened dialog.
- So, the site loads, you click a link, and it opens in a dialog. That's fine. You can close and open it as many times as you want.
- While it's open, if you click on one of the links from the loaded content, it doesn't work.
- An Ajax GET request IS performed, but the resulting content is not successfully loaded into the dialog. (Firebug shows the request)
- The previous dialog title and dialog content is erased. But the new content is not shown, NOR is it inserted into the DOM. (The generated source does not show content anywhere.)
The links look like this...
<a href="http://www.example.com/index.php?action=something&search=somethingelse#fragment" rel="dialog" title="Title Attribute">
The click event is bound...
$('body').delegate("a[rel~=dialog]", "click", function(e){return ajax_dialog(this, e);});
The ajax_dialog
function checks to see if there's a dialog, calls to create one if there isn't, calls to load the content, sets the title, and opens the dialog if it's not open.
function ajax_dialog(_this, _event){
var urlToLoad = $(_this).attr("href").replace("#", "&ajax=true #");
var linkTitle = $(_this).attr("title");
// Create dialog
if(!$('body').find('#ajaxDialog').size()){
$('body').append('not yet init<br />'); // This shows up the first click only.
init_dialog('#ajaxDialog');
}
// Load Dialog Content
load_dialog('#ajaxDialog', urlToLoad);
// Add title
$('#ajaxDialog').dialog('option', 'title', linkTitle);
// Open dialog (or reload)
if(!$('#ajaxDialog').dialog('isOpen')){
$('#ajaxDialog').dialog('open');
$('body').append('not yet open<br />'); // This shows up the first click only.
}
return false;
}
The init_dialog
function creates the dialog if there isn't one...
function init_dialog(_this){
$('body').append('<div id="ajaxDialog"></div>');
// Set Dialog Options
$(_this).dialog({
modal:true,
autoOpen:false,
width:900,
height:400,
position:['center','center'],
zIndex: 9999,
//open:function(){load_dialog(this, urlToLoad);}, This didn't work without destroying the dialog for each click.
close:function(){$(this).empty();}
});
}
The load_dialog
function loads the desired content into the dialog.
function load_dialog(_this, urlToLoad){
$(_this).load(urlToLoad, function(){
$('body').append(urlToLoad + ' load function<br />'); // This shows up each click
$(_this).append("Hihi?"); // This shows up each click
});
// The loaded information only shows the first click, other times show an empty dialog.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈。如代码所示,我使用
$jQuery.load()
并将链接的确切href
拉为 URL 请求。所有 URL 末尾都有片段/锚点,即: ....html#id-of-div
。在本例中,脚本本身工作正常,但页面上尚不存在#id-of-div。这就是为什么我可以看到返回的内容,但对话框最终变成空白。 :-)
Hah. As shown in the code, I was using
$jQuery.load()
and pulling the exacthref
of the link as the URL to request. All the URLs had fragments/anchors on the end, that is: ....html#id-of-div
.In this case, the script itself was working fine, but the #id-of-div didn't exist on the page yet. That's why I could see content returned, but the dialog just ended up blank. :-)