2012-11-21 89 views
0

的index.phpjQuery UI:如何打開/關閉由另一個對話框打開的對話框?

// load jquery.js and jquery.ui.js 
$("#dialog_a").load("template_a.php").dialog(); 
$("#dialog_b").load("template_b.php").dialog(); 

template_a.php

just HTML, no JS 

template_b.php

// !!! -> load jquery.js and jquery.ui.js <- !!! 
$("#dialog").load("template_c.php").dialog({modal:true}); 

正如你所看到的,我打開兩個對話框。 #dialog_a僅加載沒有任何函數的文本但是#dialog_b再次加載包含jquery和jquery.ui的獨立腳本。

打開#dialog_b(通過anchor/onclick)後,無法訪問index.php中的對象(例如關閉對話框)。對我來說,template_b.php好像覆蓋了DOM。但我不知道如何解決這個問題。你有?

在此先感謝!

+1

如果您已經在主頁面中包含jQuery和jQueryUI,請不要再次加載jQuery和jQueryUI。把它從你的template_b.php –

+0

@adamb:你是什麼意思? –

+0

@adamb我不得不穀歌,但現在我知道這個社區是如何工作的。謝謝你的建議! –

回答

0

解決方法:使用jQuery live() function

的index.php

$(".anchor_in_dialog_or_not_doesnt_matter").live("click", function() { 
    $("#dialog").dialog(); 
}); 

template_b.php

if ($_GET["standalone"]) 
    // load jquery, jquery.ui 
endif; 

我希望它可以幫助別人!

編輯:作爲adamb已在下面提到,請使用on()而不是live()!

+1

.live()已棄用。使用.on()代替。 [如何在jQuery文檔中將live()轉換爲on()](http://api.jquery.com/live/) – adamb

+0

@adamb感謝您的諮詢! –