2012-05-09 96 views
1

我使用.live()中,即時通訊工作的網站,而現在看來,這不是運作良好,這是我的代碼:jQuery .live()問題,無法正常工作?

$('.comment_cheer').live("click",function(e){ 

    e.preventDefault(); 

    var id   = $(this).attr('rel'); 
    var url   = $(this).attr('href'); 
    var thisClass = this; 

    if(like2 == true){ 
    like2 = false; 

     setTimeout(function(){ 
      $.post(url,function(data){ 

       $(thisClass).fadeOut('slow').fadeIn(); 
       $(thisClass).removeClass('remove'); 
       $(thisClass).addClass('add');        
       $('#boo_click_'+id).hide(); 

       like2 = true; 
      });  
     },500); 
    } 

    return false; 

}); 

我想要的是後,我發表評論,我可以選擇點擊在Cheer和Boo之間,當我點擊其中一個例如Cheer時,可點擊的Boo將消失以防止用戶點擊它,但是它不工作,我想讓它工作。有人能指導我在這裏嗎?

這裏是用我的歡呼和噓聲鏈接代碼IM:

<!--Cheer!--> 
<span id="cheer_click_<?php echo $all_post['comment_id'];?>" class="add" rel="<?php echo $all_post['comment_id']?>"> 
    <a href ="<?php echo base_url().'socket/likes/'.$all_post['comment_id']?>" 
     class ="comment_cheer" 
     rel  ="<?php echo $all_post['comment_id']?>" 
     target ="num_likes_<?php echo $all_post['num_likes']?>" 

    ><span><i class="cheer_icon button_icons_profile_13 padr5"></i>Cheer!</span></a> 
</span> 
<!--Boo!--> 
<span id="boo_click_<?php echo $all_post['comment_id'];?>" class="remove red" rel="<?php echo $all_post['comment_id']?>"> 
    <a href ="<?php echo base_url().'socket/unlike/'.$all_post['comment_id']?>" 
     class ="comment_boo" 
     rel  ="<?php echo $all_post['comment_id']?>" 
     target ="num_boos_<?php echo $all_post['num_likes']?>" 

    ><span><i class="cheer_icon button_icons_profile_14 padr5"></i>Boo!</span></a> 
</span> 
+3

您不需要'$ .live',因爲點擊事件出現泡沫,而且您似乎沒有動態添加「.comment_cheer」元素。另外,'$ .live'已被棄用。最後,我沒有看到類'comment_cheer'的一個元素 –

+0

不知道問題是什麼,但考慮移動到'on()',因爲'live()'基本被棄用,但它可能贏了不能解決您遇到的問題,並且由於我沒有閱讀流體PHP變量,因此很難說出發生了什麼事情! – adeneo

+0

@JuanMendes,我編輯了我的帖子..看看, – bunsiChi

回答

0

你定義like2某處單擊處理外,並給它的價值true?如果它不是真的或沒有定義,Boo在您點擊Cheer時不會消失。檢查這fiddle。我已經禁用了ajax帖子。

問題也可能是您的ajax發佈失敗,並因此回調函數無法運行。在回調中執行一個控制檯日誌以確保調用該函數。