2014-11-14 1 views
1

jquery와 같은 버튼을 쓰고 있지만 조건에 문제가 있습니다.jquery는 조건이있는 경우 텍스트/숫자를 올바르게 변경하지 않습니다.

내가 가진

다른 문제는 TOTAL_LIKES의 카운터입니다 : 내가 좋아하는 버튼을 클릭 카운터가 0으로 설정되어 달리 클릭하지만 난 다시 좋아하는 경우에 테 카운터가 ... 2 대신에 1

PHP는 간다 경우/HTML :

if (total_likes > 0) { 
    $hide = 'block'; 
} $hide = 'none'; 

$photos_box .= '<input type="hidden" class="tl' . $imgID . '" value="' . $total_likes . '" /> 
<a id="' . $imgID . '" class="like_button" title="' . $like_title . '">' . $like . '</a> 

//the counter 
<a id="" style="display: '. $hide . '"> 
    <span id="color" class="mod' . $imgID . '">' . $total_likes . '</span> 
</a> 
// the text 
<li id="counter" style="display: ' . $hide . ';"> 
    <div id=""> 
     <span class="text_color">' . $text . '</span> 
    </div> 
</li> 

JQuery와 스크립트 :

$('.like_button').click(function() { 

    var total_likes = $('.tl'+this.id).val(); 
    total_likes = parseInt(total_likes); 

    var status = ''; 

    if ($(this).html() == 'Like') { 
     status = 'like'; 

     $(this).html('Unlike'); 
     $(this).attr('title', 'Unlike this'); 
     $('.mod'+this.id).html(total_likes+1); 

     if (total_likes == 0) { 
      $('#counter').slideToggle('fast'); 
      $('.text_color').html('Like this.'); 
     } else if (total_likes == 1) { 
      $('.text_color').html('You and <a id="A_112">other</a> like this.'); 
     } else if (total_likes > 2) { 
      var tl = total_likes+1; 
      $('.text_color').html('You and <a id="A_112">'+tl+' others</a> like this.'); 
     } 
    } else if ($(this).html() == 'Unlike') { 
     status = 'unlike'; 

     $(this).html('Like'); 
     $(this).attr('title', 'Like this'); 
     $('.mod'+this.id).html(total_likes-1); 

     if (total_likes == 0 || total_likes == 1) { 
      $('#counter').slideToggle('fast'); 
     } else if (total_likes == 2) { 
      $('.text_color').html('<a id="A_112">1 person</a> like this.'); 
     } else if (total_likes > 2) { 
      var tl = total_likes-1; 
      $('.text_color').html('<a id="A_112">'+tl+' people</a> like this.'); 
     } 
    } 

    var data = { 
     img_id : this.id, 
     sta : status 
    }; 

    $.ajax({ 
     type : 'POST', 
     url : '/includes/like.php', 
     data : data 
    }).done(function(result) { 
     console.log(result); 
    }); 
}); 

JQuery와 실시간으로 변경 작업 내가 조건문이 무엇을 필요로하는지 할 올바른지 확실하지 않다 ... 엉망입니다. 하고있다 -2 like_counter + 1은 2를하고있다 like_counter-1 왜

나는 내가 잘못 무엇

... 몰라?

솔직히
var total_likes = $('.mod'+this.id).html(); 
+0

'경우 (TOTAL_LIKES> 0) { $는 = '블록'을 숨기기; } $ hide = 'none'; '의미가 없습니다 – epascarello

+0

왜? 만약 내가 좋아하지 않는다면 그걸 보여주고 싶지 않아. 아이콘이없고 숫자/텍스트가없는 빈 공간입니다. –

+0

음, 코드에 따르면 모든 것이 숨겨집니다. 그게 내가 본 적이없는 이상한 PHP 구문이 아니라면 말이다. – epascarello

답변

1

당신은 당신의 실제 카운터를 사용하여 ... 두 번 $ TOTAL_LIKES를 인쇄 할 필요가 없습니다 지저분한 코드 소나 이후로 이어질 것입니다.

발생한 문제는 업데이트 된 값을 숨겨진 필드에 할당하지 않았기 때문입니다. 그래서 궁극적으로 한 번에 대한 값을 교체하지만 다시

$('.tl'+this.id).val(); 

업데이트되지 않는 필드의 값을 얻고있다 그래서 당신은이 것이

total_likes = total_likes+1 
$('.mod'+this.id).html(total_likes); 
$('.tl'+this.id).val(total_likes); 

희망처럼 뭔가를하실 수 있습니다 도움

해피 학습 :)의

0

말하기, 당신은 멋지 같은 일부 템플릿 엔진이나 같이 작업하는 동안 다른를 사용할 수 있습니다 :

관련 문제