2015-09-15 93 views
0

我有以下的jQuery代碼,調整窗口Load事件的產品格:重新初始化的jQuery之後AJAX

$j(window).load(function(){ 
    // Remove list class if thumbview 
    if ($j("ul#products-list").hasClass("thumb_view")) { 
    $j("ul#products-list").removeClass("list") }; 
    // Equalize all thumb heights on switch if list view default 
    var maxHeight = 0; 
    $j('ul.thumb_view div.product-container').each(function() { maxHeight = Math.max(maxHeight, $j(this).height()); }).height(maxHeight +8); 
    // Undo equalized heights for list 
    $j('ul.list div.product-container').css("height","auto"); 
}); 

在我們的過濾產品的方式在網頁例如基於價錢。當客戶調整價格範圍時,AJAX呼叫會照顧產品的實際過濾。但是,jQuery腳本不會再次運行,並且產品網格無法正確加載。我已經做了大量的研究並發現可以解決這個問題的解決方案;使用「m-ajax-after」事件,或者使用jQuery委託函數。

第一種選擇將涉及這一段代碼:

jQuery(document).bind('m-ajax-after', function (e, selectors, url, action) { 
    // reinitialize your script 
}); 

我還沒有設法得到它的工作。關於這個功能的知識是非常有限的。

在我看來,第二個選項對實際成功有最大的機會,但是我沒有能夠將它重現爲實際工作的代碼。很多主題都可以找到,我可以將它與(window).load函數結合起來。什麼是正確的方式來做到這一點?

+0

爲什麼不把你目前擁有的代碼另一個函數中的窗口加載函數可以在窗口加載時調用,並且AJAX成功? –

回答

0

很難沒有看到完整的代碼說,但這裏的一個想法:

function formatGrid() { 
// Remove list class if thumbview 
if ($j("ul#products-list").hasClass("thumb_view")) { 
    $j("ul#products-list").removeClass("list") }; 
    // Equalize all thumb heights on switch if list view default 
    var maxHeight = 0; 
    $j('ul.thumb_view div.product-container').each(function() { maxHeight = Math.max(maxHeight, $j(this).height()); }).height(maxHeight +8); 
    // Undo equalized heights for list 
    $j('ul.list div.product-container').css("height","auto"); 
} 

$j(window).load(function(){ 
    formatGrid(); 
}); 

然後調用Ajax的成功,這樣的功能:

$.ajax({ 
    //your current ajax code 
    //and then call the formatting function 
    success: function() { 
     formatGrid(); 
    } 
}); 
相關問題