简体   繁体   English

jQuery警报问题?

[英]JQuery Alert Problem?

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");. 我正在尝试使以下代码仅在特定页面上显示,但是当我单击某些页面(如主页或不使用此代码的任何其他页面)时,我得到以下`alert(“发生一些错误,请尝试稍后再试”);。

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? 在不使用此JQuery代码但仍能正常工作的页面上,如何使代码不显示警告框?

I'm using JQuery, PHP & MySQL. 我正在使用JQuery,PHP和MySQL。

Here is the JQuery code. 这是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");
            }
        });

    });
});

Try this, using an if to check if the id="rating-text" element is there: 尝试此操作,使用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");
            }
        });

    });
});

You are calling your functions for every document, because they're not triggered by a specific selector. 您正在为每个文档调用函数,因为它们不是由特定的选择器触发的。 Why not simply remove those calls to getRating() and getRatingText() outside of the $('#ratelinks li a').click()? 为什么不简单地在$('#ratelinks li a')。click()之外删除对getRating()和getRatingText()的调用?

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? 在不使用此JQuery代码但仍能正常工作的页面上,如何使代码不显示警告框?

They do use it, that's the problem. 他们确实使用它,这就是问题所在。 One option is to move everything inside a function and only call the javascript function in the pages that need this code to run. 一种选择是将所有内容移至函数内,并且仅在需要运行此代码的页面中调用javascript函数。 If you have a php include like a header.inc you need to pass some parameter to your head and call the function only when you need it. 如果您有包含header.inc之类的php,则需要将一些参数传递给头部,并仅在需要时才调用该函数。

Tbere are different approaches to this problem: Tbere是解决此问题的不同方法:

  1. Try inserting the code into a js file and include it via <script src='' /> with the help of PHP. 尝试将代码插入js文件,并在PHP的帮助下通过<script src='' />将其包括在内。
  2. Use a switch for every page that should include this script. 对于应该包含此脚本的每个页面都使用一个开关。
  3. Use an absolute url for the update.php instead of a relative one. update.php使用绝对网址,而不是相对网址。

For what I imagine you have, you need number 2. In a few minutes will post code about this. 对于我想像的您,您需要数字2。几分钟后,将发布有关此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM