2017-04-20 28 views
1

私のDjangoプロジェクトのようなボタンを実装しましたが、変更するには、ページ! 気に入った直後に嫌いなボタンを表示する方法とその逆?djangoのjsを使って "likeボタン"を切り替える方法

は、ここでは、コード

HTMLだ

<p> 
    <strong id="like_count">{{ post.likes }}</strong> people like this category 

{% if user.is_authenticated %} 
    <button id="likes" data-post_id="{{post.id}}" class="btn btn-primary" type="button"> 
    <span class="glyphicon glyphicon-thumbs-up"></span> 
     </button> 

{% endif %} 
</p> 

JS

$('#likes').click(function(){ 
    var postid; 
    postid= $(this).attr("data-post_id"); 
    $.get('/blog/like_post/', {post_id: postid}, function(data){ 
       $('#like_count').html(data); 
       $('#likes').hide(); 
    }); 
}); 
$('#likes').click(function() { 
    $('#display_advance').toggle('1000'); 
    $("i", this).toggleClass("glyphicon glyphicon-thumbs-down glyphicon glyphicon-thumbs-up"); 
}); 

は、私はあなたがに問題があると思う

+0

これはDjangoと何が関係していますか? –

+0

私はこれを私のdjangoプロジェクトで使用しています – Bharat

答えて

0

ありがとう。これは何ですか?私は内部にiタグを見ない。

第2に、あなたが.toggleClass("glyphicon glyphicon-thumbs-down glyphicon glyphicon-thumbs-up")になったとき、私はあなたがサムズダウンとサムズアップの両方を挙げるべきではないと思います。これは、Djangoのに関連していない、それだけでjqueryの PS2だ:なぜあなたは#likesに2つの連続でクリックイベント持たない代わりに、あなたは2文 .toggleClass("glyphicon-thumbs-up") .toggleClass("glyphicon-thumbs-down")

PSを持っている必要がありますか?

0

私はシンプルで実験的にこれを実践するのが簡単です。

<button id="acceptMe" onclick="hideAccept();">Accept</button> 
<button id="rejectMe" onclick="hideReject();">Reject</button> 

<script> 
function hideAccept(){ 
document.getElementById("acceptMe").style.display="none"; 
document.getElementById("rejectMe").style.display="block"; 
} 

function hideReject(){ 
document.getElementById("rejectMe").style.display="none"; 
document.getElementById("acceptMe").style.display="block"; 
} 
</script> 
関連する問題