0

好的,所以我在我的博客上建立了一个投票系统。很像stackoverflow上的那个。该系统在博客卷的第一篇文章上运行良好,但在其他每一篇文章中,点击图片时图像都没有改变,但实际投票过程仍在进行中。

这是为什么?任何帮助都会很棒!

这是我的 JS:

function upvote(id)
  {
    sender = new XMLHttpRequest();
    if(document.getElementById("upvoteimg").src.indexOf("voted") == -1)  //have they already voted up?
    {
      document.getElementById("upvoteimg").src = "assets/upvoted.png"; // change to upvoted
      sender.open("GET","/like/index/" + id ,true);
      sender.send();
      document.getElementById("likes").innerHTML = parseInt(document.getElementById("likes").innerHTML) +1; //update #
    }
  else
    {
      document.getElementById("upvoteimg").src = "assets/upvote.png"; //change to not upvoted
      sender.open("GET","/like/index/" + id ,true);
      sender.send()
      document.getElementById("likes").innerHTML = parseInt(document.getElementById("likes").innerHTML) - 1; //update #
    }

  }

  function downvote(id)
  {
    sender = new XMLHttpRequest();
    if(document.getElementById("downvoteimg").src.indexOf("voted") == -1) //have they already downvoted?
    {
      document.getElementById("downvoteimg").src = "assets/downvoted.png"; //change to downvoted
      sender.open("GET","/like/down/" + id ,true);
      sender.send()
      document.getElementById("likes").innerHTML = parseInt(document.getElementById("likes").innerHTML) - 1; //update #
    }
  else
    {
      document.getElementById("downvoteimg").src = "assets/downvote.png"; //change to not downvoted
      sender.open("GET","/like/down/" + id ,true);
      sender.send()
      document.getElementById("likes").innerHTML = parseInt(document.getElementById("likes").innerHTML) +1; //update #
    }
  }

以下是我认为的按钮:

<!-- Voting System -->
<div id="post-vote">
    <% #DownVote %>
        <%if user_signed_in?%>  
            <%if Dislikes.where(:user_id => current_user.id, :article_id =>a.id).size == 0%>
                <%= image_tag("downvote.png", "onclick" => "downvote(#{a.id})", "id" => "downvoteimg") %>
            <%else%>
                <%= image_tag("downvoted.png", "onclick" => "downvote(#{a.id})", "id" => "downvoteimg") %>
            <%end%>
        <%else%>
            <%= image_tag("downvote.png", "onclick" => "alert('You must login to vote.')", "id" => "downvoteimg") %>
        <%end%>
    <% #TheNumber %>
    <span id="likes" class="vote-count"><%= a.likes.size - Dislikes.where(:article_id =>a.id).size%></span>

    <% #UpVote %>
        <%if user_signed_in?%>  
            <%if !a.likes.include?(current_user)%>
                <%= image_tag("upvote.png",  "id" => "upvoteimg", "onclick" => "upvote(#{a.id})") %>
            <%else%>
                <%= image_tag("upvoted.png",  "id" => "upvoteimg", "onclick" => "upvote(#{a.id})") %>
            <%end%>
        <%else%>
            <%= image_tag("upvote.png", "id" => "upvoteimg", "onclick" => "alert('You must login to vote.')") %>
        <%end%>
</div>
4

1 回答 1

0

将 js 中的所有图像更改为具有唯一document.getElementById("upvoteimg")IDdocument.getElementById("upvoteimg" + id)

以及视图中的所有图像<%= image_tag("downvoted.png", "onclick" => "downvote(#{a.id})", "id" => "downvoteimg#{a.id}") %>

于 2012-08-20T22:53:42.533 回答