2011-03-25 41 views
0

我有以下代碼,我需要一些幫助,我想用jQuery實現同樣的事情。我發現了一些接近的解決方案,但仍然在尋找完美的解決方案。一個函數加載各種php頁面與jQuery和AJAX

function getData(dataSource, divID) 
    { 
if(XMLHttpRequestObject) { 
var obj = document.getElementById(divID); 
XMLHttpRequestObject.open("GET", dataSource); 
XMLHttpRequestObject.onreadystatechange = function() 
{ 
if (XMLHttpRequestObject.readyState == 4 && 
XMLHttpRequestObject.status == 200) { 
obj.innerHTML = XMLHttpRequestObject.responseText; 
} 
} 
XMLHttpRequestObject.send(null); 
} 

} 

現在我觸發功能有:

<input type = "button" value = "TEST" onclick = "getData('subject_selectAJAX.php?course_id=1', 'ajax')">

我想實現與jQuery同樣的事情。我認爲下面的函數雖然錯了,但我的問題是,URL的變化取決於用戶在課程中的位置。 course_select.php最初裝入#ajax股利,其然後將與subject_select.php更換?COURSE_ID = 「任何 」後跟topic_select.php?subject_id =「 任何

function getData() { 

    //generate the parameter for the php script 
    $.ajax({ 
     url: "something.php", //i want this to be passed to the function 
     type: "GET",   
     data: data,  
     cache: false, 
     success: function (html) { 

      //add the content retrieved from ajax and put it in the #ajax div 
      $('#ajax').html(html); 

      //display the body with fadeIn transition 
      $('#ajax').fadeIn('slow');  
     }  

} ); }

我會喜歡這個幫助,我目前很困惑,很確定它會幫助其他Ajax jQuery新手。

回答

0

這似乎工作,雖然我敢肯定它可以更乾淨,我仍然想要爲較慢的連接添加某種加載圖形。

<script type="text/javascript"> 

function getData(url, id){ 
    $.ajax({ 
      type: "GET", 
      url: url, 
      data: "id=" + id, 
    cache: false, 
      success: function(html){ 
        $('#ajax').hide(); 

        //add the content retrieved from ajax and put it in the #content div 
        $('#ajax').html(html); 

        //display the body with fadeIn transition 
        $('#ajax').fadeIn('fast'); 



     }) 

}; 

</script>