2013-02-15 136 views
0

我想將ajax響應加載到colorbox中。 colorbox應該在點擊時打開並顯示加載圖像,直到獲取並顯示內容。將ajax內容加載到colorbox中

我的代碼:

jQuery(".likeBar").colorbox({initialWidth:'460px', initialHeight:'355px', width:'460px', height:'355px', overlayClose:true, html: function() { 
    var eventID = jQuery(this).data("id"); 
    var sp_response = ""; 
    var the_data = 
    { 
     action: 'get_likes', 
     eventID: eventID, 
     }; 
    jQuery.post(ajaxurl, the_data, function(sp_response) { 
     return sp_response; 
    }); 
} 
}); 

我的問題: 將抓取的內容將不會被顯示在我的顏色框。 AJAX調用成功,內容被提取,但不顯示。

我的問題: 如何將內容從ajax調用插入到colorbox?

回答

1

的解決我的問題:

jQuery(".likeBar").click(function(){ 
    var eventID = jQuery(this).data("id"); 
    var sp_response = ""; 
    var the_data = 
    { 
     action: 'get_likes', 
     eventID: eventID, 
     tb_check_code: tb_check_code 
    }; 
    jQuery.post(ajaxurl, the_data).done(function(sp_response) { 
     jQuery('#sp_likebox' + eventID).html(sp_response); 
    }); 

    jQuery('#sp_likebox' + eventID).show(); 
    jQuery(this).colorbox({initialWidth:'460px', initialHeight:'355px', width:'460px', height:'355px', overlayClose:true, inline:true, href: '#sp_likebox' + eventID, title: 'Diesen Benutzern gefällt der Beitrag'}); 

    jQuery(this).colorbox({ 
     onCleanup:function(e){ 
      jQuery('#sp_likebox' + eventID).hide(); 
     } 
    }); 
}); 
0

EDIT 2

我讀過顏色框文檔,發現可以用ajax通過只提供href屬性和數據屬性來獲取內容。

http://www.jacklmoore.com/colorbox

編輯 儘量讓Ajax調用異步假像這樣

$.ajax({ 
    type: "POST", 
    url: ajaxurl, 
    data: the_data, 
    success: function(sp_response) { 
     return sp_response; 
    }, 
    dataType: 'html', 
    async: false 
}); 
+0

我想要的顏色框下點擊後立即打開而不是在ajax調用完成後。 – Chris 2013-02-15 12:39:05

+0

然後嘗試第二種方法,並用我的答案中的替換Jquery.post簡寫函數。它會加載內容,因爲它會同步! – Ateszki 2013-02-15 12:41:50

+0

但是這會導致瀏覽器停滯,用戶無法做其他任何事情。 – Chris 2013-02-15 12:44:44