【发布时间】:2010-02-18 22:11:03
【问题描述】:
我试图让以下代码仅显示在特定页面上,但是当我单击某些页面(如主页或任何其他不使用此代码的页面)时,我得到以下 `alert("发生了一些错误, 请稍后再试”);.
如何让代码不在不使用此 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");
}
});
});
});
【问题讨论】:
-
是否有一个 php 代码可以为我执行此操作,因为我在我的 php 包含中包含此代码。