jQuery ajax在Chrome中工作,但不是Firefox或IE


jQuery ajax works in Chrome but not Firefox or IE

公平警告:我不是专家,但我确实做到了这一点。我的代码不美观,而且很粗糙。这是一个相当复杂的系统,所以不要不好意思问问题。

所以我有一个恼人的问题,我的代码在chrome工作,但没有其他地方。似乎我的javascript在Firefox或IE中都不起作用。请注意,每次你看到PHP在DIV中,它只是表示数据库中文章的#号。

我的代码显示的帖子,其中每个帖子配对一个喜欢和不喜欢的按钮构建跨度。有一个复选框,显示/隐藏所有喜欢的帖子和另一个做同样的不喜欢的帖子时,选择。当用户通过点击按钮喜欢或不喜欢一个帖子时,值将通过ajax(到check.php)发送到我的数据库,以便将来访问时可以调用它们。

同样,它在Chrome中运行良好,但在IE和Firefox中不行。

另外,除非我首先手动将值插入到数据库中的userPosts表中,否则不会在数据库中保存新的帖子和值。例如,如果我的数据库已经有帖子1-3的值,所有未来的决定由用户喜欢/不喜欢这些帖子发送和保存没有问题,但如果我添加一个新的帖子(post4)和用户喜欢或不喜欢它,没有值被发送…似乎INSERT在check.php中不起作用,而UPDATE功能却很好。

这是位于循环内的jQuery,您应该会发现它的注释令您满意:

<script type="text/javascript">
$(document).ready(function() {
// Declare variables
    var checked = <?php echo $row['value']; ?>; //get value of Liked or Disliked from database
    var postID = <?php echo $row['postID']; ?>; //get post ID from database
    var userID = <?php echo $current_user->ID; ?>; //get the wordpress user's ID
    var showLikes = $("input[name*='show_likes']"); //represents checkbox for Hide Liked
    var showDislikes = $("input[name*='show_dislikes']"); //represents checkbox for Hide Disliked
// Set the remembered Liked and Disliked buttons
    if (checked == 1) {
        $('#post_<?php echo $row['postID']; ?>').addClass('like'); 
        $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
    } else if (checked == 0) {
        $('#post_<?php echo $row['postID']; ?>').addClass('dislike'); 
        $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
    }
//When Liked button is clicked do this
$('#like_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '1';
// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });
// If post is Disliked, change to Liked
    $('#post_<?php echo $row['postID']; ?>').removeClass('dislike').addClass('like');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgon').addClass('dislikeimgoff');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
// If Hide Liked checkbox is on, toggle the post
    if (showLikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }
    return false;
});
//When Disliked button is clicked do this
$('#dislike_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '0';
// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });
// If post is Liked, change to Disliked
    $('#post_<?php echo $row['postID']; ?>').removeClass('like').addClass('dislike');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgon').addClass('likeimgoff');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
// If Hide Disliked checkbox is on, toggle the post
    if (showDislikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }
    return false;
});
//When Hide Liked checkbox clicked, toggle all Liked posts.
    $("input[name*='show_likes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.like')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });
//When Hide Disliked checkbox clicked, toggle all Disliked posts.
    $("input[name*='show_dislikes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.dislike')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });
});
</script>

下面是每个帖子的代码,也位于循环中,后面跟着#Message,当ajax返回check.php的输出并最终关闭循环时出现:

<div id="post_<?php echo $row['postID']; ?>" class="post">
<div id="post_<?php echo $row['postID']; ?>_inside" class="inside">
    <div id="like">
        <a id="like_<?php echo $row['postID']; ?>" class="likeimgoff" href="#"><span></span></a>
    </div>
    <div id="dislike">
        <a id="dislike_<?php echo $row['postID']; ?>" class="dislikeimgoff" href="#"><span></span></a>
    </div>
    <b><?php echo $row['Title']; ?></b><br>
    <?php echo $row['Description']; ?><br>
</div>
</div>
<div id="Message_<?php echo $row['postID']; ?>" class="reminder"></div>
<?php 
} 
?>
</div>

这里是check.php:

<?php
mysql_connect("name.database.com", "username", "password") or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error());
if (isset($_POST['userID'])){
$userID = mysql_real_escape_string($_POST['userID']);   
}else{
echo "No userID";
}
if (isset($_POST['postID'])){
$postID = mysql_real_escape_string($_POST['postID']);   
}else{
echo "No postID";
}
if (isset($_POST['value'])){
$value = mysql_real_escape_string($_POST['value']);   
}else{
echo "No value";
}
$query = mysql_query("SELECT * FROM userPosts WHERE userID='$userID' AND postID='$postID';") or die(mysql_error()); 
if (mysql_num_rows($query) > 0) { 
mysql_query("UPDATE userPosts SET value='$value' WHERE userID='$userID' AND postID='$postID';") or die(mysql_error());
} else {
mysql_query("INSERT INTO userPosts (userID, postID, value) VALUES ('$userID', '$postID', '$value') ") or die(mysql_error()); 
} 
echo "UserID: " .$userID. " PostID: " .$postID. " Value: " .$value;
?>

就是这样了。我知道这是很多代码,所以请不要害羞,随时提问!

对不起,我没有花时间阅读你的整个帖子,但通常这种类型的问题是一个javascript错误。如果没有,请在Firefox上安装Firebug,并在Firebug控制台中查看是否有任何错误,还要查看是否发出了ajax调用以及得到了什么答案。