2011-11-17 61 views
0

第一篇文章,即時通過使用cakephp框架與Ajax創建一個回覆按鈕。問題在於當用戶點擊回覆按鈕時,一旦文本框繼續加載。我怎樣才能讓它切換,以便當用戶第二次點擊它時,它會關閉回覆文本框。現在我可以使用thiss.remove()來禁用按鈕。但id更接近文本框。我的代碼在下面。感謝切換jquery和cakephp

<script type="text/javascript"> 
jQuery('.reply-button').live('click', function() { 
    var a = jQuery(this).data('comment_id'); 
    var b = jQuery(this).data('video_id'); 
    var c = jQuery(this).data('comment_quote'); 
    var d = jQuery(this).data('reply_name'); 
    var e = jQuery(this).data('quote_body'); 
    var f = jQuery(this).data('topic_name'); 
    var thiss = jQuery(this); 
    jQuery.ajax({ 
     type: "POST", 
     url: "<?= $this->Html->url(array('controller' => 'comments', 'action' => 'reply')); ?>", 
     data: { comment_id: a, video_id: b, comment_quote: c, reply_name: d , quote_body: e , topic_name: f }, 
     success: function(html) { 
      thiss.parent().parent().parent().append(html); 
     } 
    }); 
}); 

回答

0

如果你的文本有我會用它來確定它是否是一個可見或id屬性class /隱藏/已加載,並基於該隱藏/顯示/加載它。

例如說文本框有reply一個id你可以這樣做:

jQuery('.reply-button').live('click', function() { 

    // Check if the textbox is on the page or if we need to load it via an ajax call 
    if (jQuery('#reply').length == 0) 
    { 
     // The textbox isn't loaded on the page so call the ajax and append it  
     var a = jQuery(this).data('comment_id'); 
     var b = jQuery(this).data('video_id'); 
     var c = jQuery(this).data('comment_quote'); 
     var d = jQuery(this).data('reply_name'); 
     var e = jQuery(this).data('quote_body'); 
     var f = jQuery(this).data('topic_name'); 
     var thiss = this; 

     jQuery.ajax({ 
      type: "POST", 
      url: "<?= $this->Html->url(array('controller' => 'comments', 'action' => 'reply')); ?>", 
      data: { comment_id: a, video_id: b, comment_quote: c, reply_name: d , quote_body: e , topic_name: f }, 
      success: function(html) { 
       thiss.parent().parent().parent().append(html); 
      } 
     });   
    } 
    else 
    { 
     // The textbox is on the page so check if its visible/showing 
     if (jQuery('#reply').is(':visible')) 
     { 
      // The textbox is visible so hide it 
      jQuery('#reply').hide(); 
     } 
     else 
     { 
      // The textbox is hidden so show it 
      jQuery('#reply').show(); 
     } 
    } 
}); 

編輯:我也改變你var thiss = jQuery(this);var thiss = this;作爲this已經是一個jQuery對象,所以它並不需要通過jQuery(this)將它變成一個。

+0

很感謝那種先生 – user1032854