PHP和IE浏览器显示问题

时间:2010-01-07 04:59:12

标签: php jquery

我似乎无法弄清楚这个问题是出于某种原因,如果在Firefox中没有投票而在Internet Explorer中没有投票我会显示我的代码(no votes cast)我想知道是否有人可以告诉我如何修复此问题,以便在两个浏览器中显示代码?

我正在使用JQuery和PHP。

这是PHP代码。

// function to retrieve average and votes
function getRatingText(){
    $sql= "SELECT * FROM vote";
    $result = mysql_query($sql);
    $rs = mysql_fetch_array($result);
    if (!empty($rs['value']) && !empty($rs['counter'])){
        $avg = (round($rs['value'] / $rs['counter'],1));
        $votes = $rs['counter'];
        echo $avg . "/10  (" . $votes . " votes cast)";
    } else {
        echo "(no votes cast)";
    }
}

JQuery代码。

$(document).ready(function() {
    // 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");
            }
        });
    }

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

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

    });
});

1 个答案:

答案 0 :(得分:1)

在JavaScript中,您对getRatingText()的第一次调用不正确(getRating()函数内的调用):

$("#rating-text").text(getRatingText());

您实际上是在执行此操作,因为您的方法不会返回任何内容:

$("#rating-text").text(undefined);

将该行更改为:

getRatingText();