2010-02-18 120 views
0

我试图让下面的代码只显示在特定页面上,但是当我点击某些页面,比如主页或者其他任何不使用这段代码的页面时,我会得到如下警告(“发生了一些错误,请稍后再试”);。JQuery警报问题?

如何在不使用此JQuery代码的页面上显示警告框并仍然有代码工作?

我正在使用JQuery,PHP & MySQL。

这里是JQuery代码。

$(document).ready(function() { 

    // get average rating 
    getRatingText(); 
    // get average rating function 
    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"); 
      } 
     }); 
    } 

    // get current rating 
    getRating(); 
    // get rating function 
    function getRating(){ 
     $.ajax({ 
      type: "GET", 
      url: "../../../update.php", 
      data: "do=getrate", 
      cache: false, 
      async: false, 
      success: function(result) { 
       // apply star rating to element dynamically 
       $("#current-rating").css({ width: "" + result + "%" }); 
       // add rating text dynamically 
       $("#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) { 
       // remove #ratelinks element to prevent another rate 
       $("#ratelinks").remove(); 
       // get rating after click 
       getRating(); 
      }, 
      error: function(result) { 
       alert("some error occured, please try again later"); 
      } 
     }); 

    }); 
}); 
+0

是否有一个PHP代码可以做到这一点,因为我在我的PHP包含这个代码。 – gReEdY 2010-02-18 22:15:57

回答

1

试试这个,使用if来检查id="rating-text"元素有:

$(document).ready(function() { 

    if($("#rating-text").length) { 
     // get average rating 
     getRatingText(); 
     // get current rating 
     getRating(); 
    } 
    // get average rating function 
    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 getRating(){ 
     $.ajax({ 
      type: "GET", 
      url: "../../../update.php", 
      data: "do=getrate", 
      cache: false, 
      async: false, 
      success: function(result) { 
       // apply star rating to element dynamically 
       $("#current-rating").css({ width: "" + result + "%" }); 
       // add rating text dynamically 
       $("#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) { 
       // remove #ratelinks element to prevent another rate 
       $("#ratelinks").remove(); 
       // get rating after click 
       getRating(); 
      }, 
      error: function(result) { 
       alert("some error occured, please try again later"); 
      } 
     }); 

    }); 
}); 
0

您正在为每个文档调用函数,因为它们不是由特定选择器触发的。为什么不简单地将这些调用移除到$('#ratelinks li a')之外的getRating()和getRatingText()。click()?

0

怎样才可以有代码不显示 网页上的警告框不使用 这个jQuery代码并仍然有 代码工作?

他们确实使用它,这就是问题所在。一种选择是将所有内容都移到一个函数中,只需要在需要此代码运行的页面中调用javascript函数。如果你有一个php头像header.inc,你需要传递一些参数到你的头上,并且只在你需要的时候调用这个函数。

0

Tbere有不同的方法解决这个问题:

  1. 通过<script src='' />尝试将代码插入到一个js文件,包括它与PHP的帮助。
  2. 为每个应包含此脚本的页面使用开关。
  3. 使用绝对网址代替update.php而不是相对的网址。

对于我想象中的你,你需要编号2.在几分钟后会发布代码。