2011-12-26 75 views
3

我想通過AJAX調用將一些數據加載到PHP腳本,然後將其返回,bla bla bla。那麼,它工作得很好,直到jScrollPane不會重新加載AJAX調用。我簡直不明白爲什麼,因爲我已經在$ .ajax調用的成功部分中調用了它......但是哦,不知道。看看下面的代碼,告訴我我做錯了什麼/如何解決它。jScrollPane和AJAX

function eventLink(){ 
jQuery(".ticket-link a").fancybox({ 
    width:710, 
    height:750, 
    type:"iframe", 
    transitionIn:"elastic", 
    transitionOut:"elastic", 
    speedIn:600, 
    speedOut:600, 
    easingIn:"easeInExpo", 
    easingOut:"easeOutExpo", 
    overlayShow:true, 
    hideOnOverlayClick:false, 
    hideOnContentClick:false, 
    overlayOpacity:0.8, 
    overlayColor:"#000", 
    showCloseButton:true, 
    titleShow:true, 
    centerOnScroll:true 
}); 
} 

function scrollPane() { 
jQuery('.scroll-pane').jScrollPane({ 
    showArrows: true, 
    autoReinitialise: true 
}); 
} 

jQuery(document).ready(function(){ 
jQuery("select[name='sortby']").change(function(){ 
    var a=jQuery(this).val(); 
    jQuery.ajax({ 
     type:"GET", 
     url:"ticket_order.php", 
     data:"sortby="+a, 
     beforeSend:function(){ 
      jQuery("#ajax-loader").show(); 
      jQuery("#ticket-list").hide(200); 
      jQuery("#ticket-list").empty()}, 
     complete:function(){ 
      jQuery("#ajax-loader").hide(); 
      eventLink(); 
     }, 
     success:function(a){ 
      jQuery("#ticket-list").html(a); 
      jQuery("#ticket-list").show(200); 
      scrollPane(); 
     } 
    }); 
    return false}); 
eventLink(); 
scrollPane(); 
}); 

回答

6

我碰到的這個問題JScrollPane中。一旦它在元素周圍創建了滾動窗格結構,就需要對它進行不同的處理。如果在函數中重新初始化,它的響應並不好。相反,你必須通過暴露的方法獲得api的參考並重新初始化。

使用您的代碼將是一個例子...

// initialise the scrollpanes 
$(document).ready(function() { 
    jQuery('.scroll-pane').jScrollPane({ 
     showArrows: true, 
     autoReinitialise: false 
    }); 
}) 

再就是您需要爲您JScrollPane的兩件事情。這是內容容器和重新初始化方法。原因似乎是,一旦你用jScrollPane()初始化一個容器,容器本身就變成了一個jScrollpane對象。內容被移動到該對象內的容器。因此,如果您替換目標div的內容,則會刪除組成jScrollPane對象的html元素。以下是調用內容窗格,填充數據並重新初始化的窗口。

api.getContentPane()將爲您提供對滾動窗格業務結束的引用,並且api.reinitialise()將重新繪製並重新計算滾動窗格。因此,要使用你的榜樣,

jQuery("#ticket-list").html(a); 
    jQuery("#ticket-list").show(200); 

將成爲:

api = jQuery("#ticket-list").data('jsp'); 
    api.getContentPane().html(a); 
    jQuery("#ticket-list").show(200); 
    api.reinitialise(); 

$(document).ready(function() { 
    // initialise the scrollpanes 
    jQuery('.scroll-pane').jScrollPane({ 
      showArrows: true, 
      autoReinitialise: false 
    }); 
    }) 
    jQuery("select[name='sortby']").change(function(){ 
    var a=jQuery(this).val(); 
    jQuery.ajax({ 
     type:"GET", 
     url:"ticket_order.php", 
     data:"sortby="+a, 
     beforeSend:function(){ 
      jQuery("#ajax-loader").show(); 
      jQuery("#ticket-list").hide(200); 
      jQuery("#ticket-list").empty()}, 
     complete:function(){ 
      jQuery("#ajax-loader").hide(); 
      eventLink(); 
     }, 
     success:function(a){ 
      api = jQuery("#ticket-list").data('jsp'); 
      api.getContentPane().html(a); 
      jQuery("#ticket-list").show(200); 
      api.reinitialise(); 
     } 
    }); 
    return false}); 
eventLink(); 
}); 

這裏是best documentation of the api因爲我能找到。

+0

嗯,似乎沒有工作。 AJAX現在沒有意義放棄數據。 'api = jQuery(「#ticket-list」)。data('jsp'); api.getContentPane()。html(a);' 這看起來合乎邏輯,但現在它不會將數據放入#ticket-list:/ – techouse 2011-12-26 12:14:02

+0

奇怪的是'console.log(api.getContentPane()。 html(a));'顯示所有返回的AJAX調用的html數據,但它不會顯示在頁面上。在AJAX調用之後,jspContainer div不會被放入標記中。 – techouse 2011-12-26 12:23:15

+0

嗯...我發現'的console.log(api.getContentPane()HTML(一));'顯示,該AJAX返回的內容缺少'

' at the beginning and '
'在結束。 – techouse 2011-12-26 13:01:19