jquery像stackoverflow一样切换投票

时间:2010-10-06 23:08:22

标签: javascript jquery html ajax toggle

  

可能重复:
  jQuery voting system

我有这个投票jquery脚本,它工作正常,但我希望用户选项切换投票,就像stackoverflow,所以基本上

you can vote up by clicking up arrow
   increase vote by 1
   if they click again on the picture(arrow up)
   minus 1 from the vote

我的jquery脚本:

$(document).ready(function() {
    $('.statuses').delegate('.vote_up', 'click', function(e) {

        //stop event
        e.preventDefault();
       //get the id
        var the_id = $(this).closest('.message').attr('id').split('_').pop();
        //the main ajax request
        $.ajax({
            context: this,
            type: "POST",
              // Make sure "this" in the callback refers to the element clicked

            data: "action=vote_up&id=" + the_id,
            url: "ajax/votes.php",
            success: function (msg) {
                  // Not sure where your vote_count is. See the HTML for my placement
                $(this).siblings("span.vote_count").html(msg).fadeIn();
                  // get the child <img> and set its src
                $(this).children("img").attr("src", "img/uparrowActive.png");
            }
        });
    });
});

html代码:

<span class="vote_count">0</span>
<a href="#" class="vote_up"><img src="img/uparrow.png" /></a>
<a href="#" class="vote_down"><img src="img/downarrow.png" /></a>

我在想什么是最好的方法来做到这一点,谢谢:))

0 个答案:

没有答案