2012-03-23 84 views
-1

我有其布局是很简单的一个页面的jQuery切换股利。所有文章(在foreach循环中)都包含一个类似.like(类似按钮)的按钮。当按下时,它很喜欢。现在让我们假设这是永久性的。如果VAR等于值不起作用

我有一个可变likestatus跟踪被按压倍#show_likes的数量。当像状态除以2没有剩余时,这意味着应该隐藏所有喜欢的帖子。如果有余额,所有喜欢的帖子应该是可见的。相当直接向前?

在页面加载,likestatus设置为1,自1 2 divded有剩余,都喜欢帖子被隐藏在页面加载。这适用于Firefox和Chrome。

因为likestatus设置为1,用户决定喜欢的所有帖子应自动切换。这工作在Firefox但不是铬。

问题是,为什么?

的JavaScript

$(document).ready(function() { 
    likestatus = 1; //on pageload, likestatus is 1 so all liked posts are hidden. 

$(document).on("click", ".like", function(){    //when like button is pressed do this 
    postID = $(this).attr('id').replace('like_', '');  // get the ID of the post 

    // Declare variables 
    value = '1';           //this represents that the post is liked to be stored in a database 

    myajax();            //send to database 

    return false; 
}); 

function myajax(){           // Send values to database 
    $.ajax({ 
     url: 'check.php',         //check.php receives the values sent to it and stores them in the database 
     type: 'POST', 
     data: 'postID=' + postID + '&value=' + value,  //send the post ID and like value 
     success: function(result) { 
      if (result.indexOf("No") < 0){     //If return doesn't contain string "No", do this 
       if (value == 1){       //If post is liked, do this 
        $('#post-' + postID).removeClass('dislike').addClass('like');    //sets div class of the post to liked 
        $('#likebtn_' + postID).removeClass('likeimgoff').addClass('likeimgon');  //changes the image of the like button so it is visibly activated 

// UP TO HERE, THE CODE WOKS IN BOTH CHROME AND FIREFOX. IN CHROME, THE CODE BELOW DOESN'T WORK 

       // If Hide Liked button is on, toggle the post 
       if (likestatus % 2 == 0) { 
       } else { 
        $('#post-' + postID).toggle(); 
       } 
      } 
     } 
    } 
}); 
} 

// THE CODE BELOW WORKS IN BOTH CHROME AND FIREFOX 

$('#show_likes').on('click', function() { //When Hide Liked checkbox clicked, toggle all liked posts. 
    likestatus++; //increment likestatus 

    if (likestatus % 2 == 0) { 
     $('#hidelikedbtn').removeClass('hidelikedimgoff').addClass('hidelikedimgon'); // changes the image of the hide all liked button so it is visibly deactivated 
    } else { 
     $('#hidelikedbtn').removeClass('hidelikedimgon').addClass('hidelikedimgoff'); // changes the image of the hide all liked button so it is visibly activated 
    } 

return false; 
}); 

的index.php

<?php global $post; ?> 

<div id="show_likes"> 
    <a id="hidelikedbtn" class="hidelikedimgoff mstrctrlL" href="#"><span></span> </a> 
</div> 

<?php foreach ($pageposts as $post): ?> 

<?php setup_postdata($post); 
     $msg_id= $post->ID; 
?> 

<div id="post-<?php the_ID(); ?>" class="post <?php post_class(); ?>"> 
    <div id="post-<?php the_ID(); ?>-inside" class="inside"> 
     <h2 class="posttitle"> 
      <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to', 'buddypress') ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a> 
     </h2> 

     <div id="like_<?php the_ID(); ?>" class="like"> 
       <a id="likebtn_<?php the_ID(); ?>" class="likeimgoff" href="#"><span></span></a> 
      </div> 

     <div class="entry"> 
      <?php the_content(__('Read the rest of this entry &rarr;', 'buddypress')); ?> 
     </div> 
    </div> <!-- post-ID-inside --> 
</div> <!-- post-ID --> 

当我改变$( '#后' +帖子ID).toggle()为$( '#后' +帖子ID)的CSS( “可见性”, “隐藏”);它的作品(尽管这个帖子只是看不见而已)。重点是,代码没有工作,一路下跌至这条线和没有帖子ID得到认可,但一些关于Chrome的就是不让切换功能..

回答

0

尝试用alert('postID');更换线$('#post-' + postID).toggle();。如果警报弹出,那么你知道成功功能会触发你的逻辑。

假设火灾警报,检查,以确保postID包含有效的值,而#post-XX是在您的标记有效id属性(其中“XX”是postID)。

另外,如果你像这样调用切换,会发生什么?

if ((likestatus % 2) > 0) { 
    $('#post-' + postID).toggle(); 
} 
+0

帖子ID设置完美的罚款。就像我说的,AJAX发送没有问题,确认消息甚至显示发送的postID。 Firefox报告该值是有效的,并且在我的标记中它是无效且有效的属性。我已经用成功函数中更多的代码更新了我的代码,以告诉你为什么我知道它是有效的......一些类被切换并且它们确实有效(类改变了按钮的图像。与所述反复.. – Sweepster 2012-03-24 02:23:38

+0

我发现我的代码不工作在Firefox,但无法在Chrome。为什么呢? – Sweepster 2012-03-24 02:44:19

+0

你可以添加一些样本标记你的问题的任何想法(如结构'“#后” + postID'? – pete 2012-03-24 03:11:36

2

你不及格的帖子ID

$(document).on("click", ".like", function(){ 
    postID = $(this).attr('id').replace('like_', ''); 

    // Declare variables 
    value = '1'; 

    myajax(postID); //added variable 

    return false; 
}); 

function myajax(postID){ // added variable 
    // Send values to database 
    $.ajax({ 
     url: 'check.php', 
     //check.php receives the values sent to it and stores them in the database 
     type: 'POST', 
     data: 'postID=' + postID + '&value=' + value, 
     success: function(result) { 
     $('#Message_' + postID).html('').html(result).prependTo('#buttons_' + postID); 
       if (result.indexOf("No") < 0){ //If return doesn't contain string "No", do this 
       if (value == 1){ //If post is liked, do this 
        // If Hide Liked button is on, toggle the post 
        if (likestatus % 2 == 0) { 
        } else { 
         $('#post-' + postID).toggle(); 
        } 
       } 
      } 
     } 
+0

postID发送得很好。我知道这是因为#Message被预先添加到了依赖于postID的正确位置。无论如何,我尝试了你的代码,但它没有解决问题。 – Sweepster 2012-03-24 02:29:16

+0

我用你的代码和问题是一样的 - 它在firefox中工作,但不在Chrome中。 – Sweepster 2012-03-24 22:05:05