2017-01-22 44 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

您可能想要更新您的问题 - 这不是您要求的问题,正如我在答复中所指出的那样(“我想在激活一个切换时禁用所有切换按钮。”) –