Problema con jquery y ajax

Tengo dos funciones:

$(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;
    });
});

One is to activate link and other is to deactivate it. Functions are working fine, but when link is activated or deactivated, it can't be clicked again to change it (page needs to be refreshed in order for functions to work again). What I need to do to make it work?

preguntado el 28 de septiembre de 11 a las 08:09

Are you changing the class of the links?! deactivated=> activated and activated => deactivated -

3 Respuestas

Cambiar:

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

A:

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

Respondido el 28 de Septiembre de 11 a las 12:09

You seem to be replacing the container and the elements inside it, so the elements that had the click events bound to them will have been removed.

You can fix this by changing your event handlers to use live:

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

y

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

Respondido el 28 de Septiembre de 11 a las 12:09

Looks like you're replacing the links that you click which loses the event bindings.

Change the .click() to use .live('click'..

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

Respondido el 28 de Septiembre de 11 a las 12:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.