jquery和ajax的问题

时间:2011-09-28 08:43:00

标签: javascript jquery

我有两个功能:

$(function() {
    $(".deactivated").click(function() {           
          var Container = $(this).parent();             
       var id = $(this).attr("id");             
        var string = 'id='+ id ;    
        $.ajax({   
            url: "<?php echo site_url('social/activate') ?>",
            type: "POST",
            data: string,
            cache: false,
                 success: function(){
                     Container.fadeOut(1000, function(){
                     $(this).load("<?php echo site_url('social/social_icon') ?>", {id: id}, function(){
                         $(this).hide().fadeIn(700);
                         $(this).click();
                     })
                 });
            }   
        });
        return false;
    });
});

 $(function() {
    $(".activated").click(function() {           
          var Container = $(this).parent();             
       var id = $(this).attr("id");             
        var string = 'id='+ id ;    
        $.ajax({   
            url: "<?php echo site_url('social/deactivate') ?>",
            type: "POST",
            data: string,
            cache: false,
                 success: function(){
                     Container.fadeOut(1000, function(){
                     $(this).load("<?php echo site_url('social/social_icon') ?>", {id: id}, function(){
                         $(this).hide().fadeIn(700);
                     })
                 });
            }   
        });
        return false;
    });
});

一个是激活链接,另一个是停用链接。功能正常,但是当激活或取消激活链接时,不能再次单击它来更改它(需要刷新页面才能使功能再次工作)。我需要做些什么来使它工作?

3 个答案:

答案 0 :(得分:3)

变化:

$(".activated").click(function() {
$(".deactivated").click(function() { 

要:

$(".activated").live('click',function() {  
$(".deactivated").live('click',function() { 

答案 1 :(得分:1)

您似乎正在替换容器及其中的元素,因此绑定了click事件的元素将被删除。

您可以通过更改事件处理程序以使用live来解决此问题:

$(".deactivated").live('click', function() { 

$(".activated").live('click', function() {

答案 2 :(得分:0)

您似乎正在替换丢失事件绑定的链接。

更改.click()以使用.live('click'..

$(".deactivated").live('click', function() {