【问题标题】:JQuery Alert Problem?jQuery 警报问题?
【发布时间】: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 包含中包含此代码。

标签: php jquery mysql


【解决方案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");
            }
        });

    });
});

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如何让代码不显示 不使用的页面上的警告框 这个 JQuery 代码,仍然有 代码工作?

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

      【讨论】:

        【解决方案4】:

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

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

        按照我的想象,你需要 2 号。几分钟后将发布有关此的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多