2013-05-01 116 views
1

嘿,我試圖關閉顯示頁面加載時顯示的div。但是,這樣做:通過Ajax加載的jQuery動態內容

jQuery(document).ready(function() { 
    jQuery('#moreDetailsTable').slideUp("fast", function() {}); 
}); 

似乎沒有關閉它,當自其被動態地經由使用AJAX PHP代碼填充頁面加載。

是Ajax調用看起來像這樣的PHP:

echo '<div id="moreDetailsTable" class="widefat fixed comments"> 
    Testing this out<br /> 
    Testing this out<br /> 
    Testing this out<br /> 
    Testing this out<br /> 
    Testing this out<br /> 
</div> 
    ect ect..... 

我知道代碼工作,因爲我只是這樣做:

jQuery(document).on('click', '#moreDetailsTable', function() { 
    jQuery('#moreDetailsTable').slideUp("fast", function() { 
     console.log('done'); 
    }); 
}); 

它向上滑動一次,我一下就可以了

我怎樣才能叫.slideUp,因爲它動態填充,而不是在頁面上開始?

+0

http://api.jquery.com/ajaxComplete/ – Jordan 2013-05-01 14:54:48

+0

@Jordan作出這樣的回答,所以我可以給你貸款,請。 – StealthRT 2013-05-01 14:57:33

+0

你能顯示你的ajax代碼嗎? – tymeJV 2013-05-01 14:57:36

回答

2

請參閱jQuery.ajaxComplete()事件處理程序。這將做你想要的。

例子:

jQuery(document).ajaxComplete(function() { 
    jQuery('#moreDetailsTable').slideUp("fast", function() {}); 
}); 
+0

再次感謝您的幫助,喬丹。 – StealthRT 2013-05-01 15:27:38

+0

沒問題!很高興我能幫上忙。 – Jordan 2013-05-01 15:28:09

0

ajax返回時調用該函數。加載內容後。例如:

$('.ajaxLoading').show(); 
    $.ajax({ 
    type:'POST', 
    url:'url', 
    dataType: "json"}).done 
    (function(response){ 
     alert(response.message); 
     $('.ajaxLoading').hide(); 
     //window.location = "/dash_board"; 
      $('#moreDetailsTable').slideUp("fast", function() {}); 
    }).fail(function(response,textStatus){ 
     alert('Something goes wrong. Please try again.'); 
     $('.ajaxLoading').hide(); 
     //alert(textStatus); 
    }); 
+0

感謝我的問題重播,何塞。 – StealthRT 2013-05-01 15:28:00