2012-01-03 79 views
0

一旦您點擊以投票表決啓用它,它只能通過單擊向下投票來禁用。我如何做到這一點,如果我點擊一個啓用的按鈕,它將被禁用。如何使用jQuery禁用/啓用按鈕/鏈接?

$("a.vote_down").click(function() { 
    //get the id 
    the_id = $(this).attr('id'); 

    //the main ajax request 
    $.ajax({ 
     type: "POST", 
     data: "action=vote_down&id="+$(this).attr("id"), 
     url: "votes.php", 
     success: function(msg) { 
      $(".vote_down").addClass("active"); 
      $(".vote_up").removeClass("active"); 
      $("span#votes_count").html(msg); 
     } 
    }); 
}); 

其中一個鏈接:

<a href='javascript:;' class='vote_up<?php if($liked["points"]=="1") echo " active";?>' id='<?php echo $id; ?>'>Vote Up</a> 

回答

2

嘗試使用此作爲對active類直播的點擊事件。增加了一些抽象。

$("a.vote_down.active").live("click", function(e) { 
    Vote($(this).attr('id'), false); 
}); 

$("a.vote_up.active").live("click", function(e) { 
    Vote($(this).attr('id'), true); 
}); 

function Vote(id, value) { 
    //the main ajax request 
    var action = value ? "vote_up" : "vote_down" 
    $.ajax({ 
     type: "POST", 
     data: "action=" + action + "&id=" + id, 
     url: "votes.php", 
     success: function(msg) { 
      $(".vote_down").toggleClass("active", value); 
      $(".vote_up").toggleClass("active", !value); 
      $("span#votes_count").html(msg); 
     } 
    }); 
} 

工作示例:http://jsfiddle.net/hunter/XQfgU/(減去AJAX)