2010-02-19 88 views
0

我只能得到getRating();getRatingText();的功能更新,而無需重新加載網頁。但其他功能getRatingText2();getRatingAvg();將不會更新,直到我重新加載網頁。JQuery更新問題?

如何在不重新加載我的網頁的情況下更新所有功能?

這裏是JQuery腳本。

$(document).ready(function() { 

    if($("#rating-text").length) { 

     getRatingText(); 

     getRatingText2(); 

     getRatingAvg(); 

     getRating(); 
    } 


    function getRatingText(){ 
     $.ajax({ 
      type: "GET", 
      url: "update.php", 
      data: "do=getavgrate", 
      cache: false, 
      async: false, 
      success: function(result) { 
       // add rating text 
       $("#rating-text").text(result); 
      }, 
      error: function(result) { 
       alert("some error occured, please try again later"); 
      } 
     }); 
    } 



     function getRatingText2(){ 
      $.ajax({ 
       type: "GET", 
       url: "update.php", 
       data: "do=getavgrate2", 
       cache: false, 
       async: false, 
       success: function(result) { 

        $("#rating-text2").text(result); 
       }, 
       error: function(result) { 
        alert("some error occured, please try again later"); 
       } 
      }); 
     } 



     function getRatingAvg(){ 
      $.ajax({ 
       type: "GET", 
       url: "update.php", 
       data: "do=getavgrate3", 
       cache: false, 
       async: false, 
       success: function(result) { 

        $("#grade-avg").text(result); 
       }, 
       error: function(result) { 
        alert("some error occured, please try again later"); 
       } 
      }); 
     } 


    function getRating(){ 
     $.ajax({ 
      type: "GET", 
      url: "update.php", 
      data: "do=getrate", 
      cache: false, 
      async: false, 
      success: function(result) { 

       $("#current-rating").css({ width: "" + result + "%" }); 

       $("#rating-text").text(getRatingText()); 
      }, 
      error: function(result) { 
       alert("some error occured, please try again later"); 
      } 
     }); 
    } 


    // link handler 
    $('#ratelinks li a').click(function(){ 
     $.ajax({ 
      type: "GET", 
      url: "update.php", 
      data: "rating="+$(this).text()+"&do=rate", 
      cache: false, 
      async: false, 
      success: function(result) { 

       $("#ratelinks").remove(); 

       getRating(); 
      }, 
      error: function(result) { 
       alert("some error occured, please try again later"); 
      } 
     }); 

    }); 
}); 

回答

0

$(document).ready函數只在頁面完成加載並且dom準備好時才被調用。 getRatingText2()和getRatingAvg()只在該函數中調用,這就是爲什麼只有在頁面加載時才調用它們的原因。

也許他們應該在點擊事件中與您一起調用其他函數或從getRating()中調用? (getRating();和getRatingText();)

+0

我在哪裏可以將此代碼放入我的代碼中我對JQuery相當陌生。 – pnut 2010-02-19 23:33:39

+0

如果您希望getRatingText2()和getRatingAvg()在點擊費率鏈接時進行更新,那麼只需將它們放在$('#ratelinks li a')。click函數中即可。 – 2010-02-19 23:42:44

+0

仍然無法正常工作。 – pnut 2010-02-19 23:46:08

0

您的「點擊」項是動態生成的嗎?如果是,請使用
$('#ratelinks li a').live('click', funcion())

+0

這也沒有工作:( – pnut 2010-02-20 00:14:01

+0

pnut,聽起來你可能有更多的jQuery問題。你確定CSS是正確的嗎?你是否嘗試過應用'live'或'click'函數來只是一個特定的鏈接ID? – Sebastian 2010-02-20 19:33:38