doumi7861 2010-02-18 22:11
浏览 32
已采纳

JQuery警报问题?

I'm trying to get the following code to display only on specific pages but when I click through certain pages like the home page or any other pages that does not use this code I get the following `alert("some error occured, please try again later");.

How can I have the code not display the alert box on pages that do not use this JQuery code and still have the code work?

I'm using JQuery, PHP & MySQL.

Here is the JQuery code.

$(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");
            }
        });

    });
});
  • 写回答

4条回答 默认 最新

  • doubi4531 2010-02-18 22:16
    关注

    Try this, using an if to check if the id="rating-text" element is there:

    $(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");
                }
            });
    
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆