Img src只通过javascript更改一次

时间:2012-08-20 22:06:07

标签: javascript ajax ruby-on-rails-3

好吧,我正在博客上建立一个投票系统。就像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>

1 个答案:

答案 0 :(得分:0)

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

以及<%= image_tag("downvoted.png", "onclick" => "downvote(#{a.id})", "id" => "downvoteimg#{a.id}") %>

视图中的所有图片