2017-01-22 79 views
0

我有一個表,物品上顯示的子行內容的每一行進行切換。在子行中,我還顯示Disqus評論。這一切工作正常,但我不希望一次打開多個行和多個Disqus評論加載,放慢我的頁面。JQuery的禁用錶行切換,當一列已經切換

我想,當一個觸發啓動禁用所有的切換按鈕。每個切換鏈接(類option_toggle)及其所在的行都有唯一的ID。見下文。我怎樣才能通過JQuery來實現這一點?

\t \t //Hide main article table's options row 
 
\t \t $(document).ready(function() { 
 
\t \t  $(".option_toggle").click(function(e) { 
 
\t \t  \t e.preventDefault(); 
 

 
\t \t  \t aid = $(this).attr('id'); 
 

 
\t \t  \t //Determine if we are showing the comments or hiding the comments 
 
\t \t  \t is_hidden = $(this).parent().parent().next(".options_row").is(":hidden"); 
 

 
\t \t  \t if(is_hidden) 
 
\t \t  \t { 
 
\t \t  \t \t disqus_container = '#disqus_container_' + aid; 
 

 
\t \t  \t \t jQuery('<div id="disqus_thread"></div>').insertBefore(disqus_container); 
 

 
\t \t \t \t \t $.ajax({ 
 
\t \t \t \t   type: 'POST', 
 
\t \t \t \t   url: 'http://www.example.com/disqus', 
 
\t \t \t \t   data: {'msmm_tn' : '12d406df3c8b2e178893e2c146d318e5', 'aid' : aid}, 
 
\t \t \t \t   dataType: 'json', 
 
\t \t \t \t   success : function(data) { 
 
\t \t \t \t    if (data) 
 
\t \t \t \t    { 
 
\t \t \t \t     \t \t $('#disqus_container_' + aid).html(data.script); 
 

 
\t \t \t \t     \t \t DISQUS.reset({ 
 
\t \t \t \t       \t reload: true, 
 
\t \t \t \t      \t config: function() { 
 
\t \t \t \t      \t   this.page.url = 'http://www.example.com/#!'+ aid; 
 
\t \t \t \t      \t   this.page.remote_auth_s3 = data.payload; 
 
\t \t \t \t      \t   this.page.identifier = aid; 
 
\t \t \t \t      \t   this.page.api_key = "cULB96iURBu1pZOtLOOSVlVgBj10SY9ctXWiv0eiQdzhdxqBq9UgmVr5SeSiaFiP"; 
 
\t \t \t \t      \t  } 
 
\t \t \t \t      }); 
 
\t \t \t \t    } 
 
\t \t \t    } 
 
\t \t \t   }); 
 
\t \t \t \t } 
 
\t \t \t \t else 
 
\t \t \t \t { 
 
\t \t \t \t \t //Remove the comments 
 
\t \t \t \t \t $('#disqus_container_' + aid).prev('#disqus_thread').remove(); 
 
\t \t \t \t \t $('#disqus_container_' + aid).html(''); 
 
\t \t \t \t } 
 

 
\t \t   $(this).parent().parent().next(".options_row").toggle("fast"); 
 
\t \t  }); 
 
\t \t }); 
 
\t \t
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<tbody> 
 
    <tr class="main_row" data-id="ROWID428272"> 
 
    <td class="bkgcol-sunflower wht-border"> 
 
    <td> 
 
    <a id="428272" class="option_toggle" href="#" title="Ratings/Comments"> 
 
    </td> 
 
    <td class="key-title dark unpadded"> 
 
    <td class="artcl_info text-center">Scitech</td> 
 
    <td class="text-center" style="width:10px"> 
 
    <td class="text-center"> 
 
    </tr> 
 
    <tr class="options_row" style="display: table-row;"> 
 
    <td colspan="6"> 
 
    <div class="row article_options"> 
 
    <div class="row comments_row"> 
 
    <div id="comments" class="col-md-12"> 
 
    <div class="text-center article_title top_pad" style="width: 100%;">Comment on This Article</div> 
 
    <div id="disqus_thread"> 
 
    <div id="disqus_container_428272"> 
 
    </div> 
 
    </div> 
 
    </td> 
 
    </tr> 
 
    <tr class="main_row" data-id="ROWID427694">

回答

0

我想禁用所有的切換按鈕,當一個觸發被激活。

$(".option_toggle").click(function(e) { 
    e.preventDefault(); 

    // Remove click event handler defined above, and add a new one which prevents 
    // the click from doing anything 
    $(".option_toggle").off('click').on('click', function(e) { 
     e.preventDefault(); 
    }); 

    aid = $(this).attr('id'); 
    // Continue with rest of your code ... 
}); 

但是,這真的是你想要的嗎?這意味着一旦你點擊一個切換,你不能點擊任何其他人。這也意味着所有其他的切換仍然看起來像鏈接,也許你應該更新的造型,表示它們將被禁止或類似的東西,如增加一個殘疾類別和風格:

的Javascript:

$(".option_toggle").addClass('disabled-link'); 

CSS:

a.disabled-link { 
    cursor: default; 
    color: #666; 
    text-decoration: none;   
} 
+0

感謝DP,我試過,但它禁用所有切換所以我甚至無法閉上電流切換/開行。我試圖找出一種方法來關閉任何可見的切換,當一個新的點擊與禁用切換。 – ReeseB

+0

您可能想要更新您的問題 - 這不是您要求的問題,正如我在答覆中所指出的那樣(「我想在激活一個切換時禁用所有切換按鈕。」) –