why my upvote button only works for the first post?

ming

I follow the instruction in tangowithdjango to add a 'like' button into my forum app.

the html file and javascript:

<script>
$(document).ready(function(){
  $("#upvote").click(function(){
    var postid;
    postid = $(this).attr("data-r-id");
    $.get('/upvote/', {post_id: postid}, function(data){
               $('#like_count').html(data);
              
    });
  });
});
</script>
<a id="upvote" data-r-id="{{r.id}}" class="label-info label pull-right">赞<div id="like_count">{{r.upvotes}}</div></a>

views.py

def upvote(request):
    post_id = None
    if request.method == 'GET':
        post_id = request.GET['post_id']
    votes = 0
    if post_id:
        p = post.objects.get(id=post_id)
        if p :
            votes = p.upvotes + 1
            p.upvotes = votes
            p.save()
    return HttpResponse(votes)

urls.py

urlpatterns = patterns(
    url(r'^upvote/$', 'upvote', name='upvote'),
)

when click the #upvote button ,the #like_count increase by one . However , it works just for the first post. If I click the rest of posts , I get no response at all.

Avinash Raj

I suggest you to give upvote as class not as id because id refers an unique element in your document.

$(document).ready(function(){
  $(".upvote").click(function(){
    var postid;
    postid = $(this).attr("data-r-id");
    $.get('/upvote/', {post_id: postid}, function(data){
               $('#like-count-' + postid).html(data);

    });
  });
});

and your html should look like,

<a data-r-id="{{r.id}}" class="label-info label pull-right upvote">赞<div id="like-count-{{r.id}}">{{r.upvotes}}</div></a>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jquery.upvote.js only works for first row

From Dev

Why this only works for the first div?

From Dev

How to make my upvote/downvote button store cookies and count votes only 1 time

From Dev

jQuery: show onclick works only for first button

From Dev

onClick only works for first button in table

From Dev

Button function only works on first click

From Dev

JQuery ajax post works only the first time

From Dev

My script works only for the first class

From Dev

only the first line of my javascript code works

From Dev

Why autofocus property works only on first trial?

From Dev

why mouseover only works for the first row

From Dev

Why JQuery function works for only the first textbox

From Dev

Why autofocus property works only on first trial?

From Dev

Why setBackground works only the first time? (JPanel)

From Dev

currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

From Dev

ActionListener for multi-dimensional array only works for first button?

From Dev

ActionListener for multi-dimensional array only works for first button?

From Dev

Delete button works on only first row not on the rest of the rows

From Dev

Why my listview only detects the first checkbox?

From Dev

Why is only my first HTTP request running?

From Dev

Why is my for only working on the first variable?

From Dev

Why is my for loop only grabbing first element?

From Dev

Why is it that only the first imported module works and the rest can't run

From Dev

Why to group list of object by an Id works only at first call in that function?

From Java

Why add class to button only works after a second click?

From Dev

Why is my list function only outputting first value of my array?

From Dev

Angular: Why my filter only works if apply the way with the lower performance (?)

From Dev

Why my processing web game works only in Chrome?

From Dev

Why my itoa implementation works only for 3 digit numbers?

Related Related

  1. 1

    jquery.upvote.js only works for first row

  2. 2

    Why this only works for the first div?

  3. 3

    How to make my upvote/downvote button store cookies and count votes only 1 time

  4. 4

    jQuery: show onclick works only for first button

  5. 5

    onClick only works for first button in table

  6. 6

    Button function only works on first click

  7. 7

    JQuery ajax post works only the first time

  8. 8

    My script works only for the first class

  9. 9

    only the first line of my javascript code works

  10. 10

    Why autofocus property works only on first trial?

  11. 11

    why mouseover only works for the first row

  12. 12

    Why JQuery function works for only the first textbox

  13. 13

    Why autofocus property works only on first trial?

  14. 14

    Why setBackground works only the first time? (JPanel)

  15. 15

    currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

  16. 16

    ActionListener for multi-dimensional array only works for first button?

  17. 17

    ActionListener for multi-dimensional array only works for first button?

  18. 18

    Delete button works on only first row not on the rest of the rows

  19. 19

    Why my listview only detects the first checkbox?

  20. 20

    Why is only my first HTTP request running?

  21. 21

    Why is my for only working on the first variable?

  22. 22

    Why is my for loop only grabbing first element?

  23. 23

    Why is it that only the first imported module works and the rest can't run

  24. 24

    Why to group list of object by an Id works only at first call in that function?

  25. 25

    Why add class to button only works after a second click?

  26. 26

    Why is my list function only outputting first value of my array?

  27. 27

    Angular: Why my filter only works if apply the way with the lower performance (?)

  28. 28

    Why my processing web game works only in Chrome?

  29. 29

    Why my itoa implementation works only for 3 digit numbers?

HotTag

Archive