简体   繁体   English

如何使用 jquery 展开/折叠表行?

[英]how to expand/collapse table rows using jquery?

I am trying to expand/collapse the following row if row with class='sector' is clicked如果单击 class='sector' 的行,我将尝试展开/折叠以下行

This same code seems to work for many people as per other stackoverflow answers but it is not working for me and I wonder why?根据其他stackoverflow答案,相同的代码似乎对很多人都有效,但对我不起作用,我想知道为什么? I do not know much jquery but this seems to me to be workable but still does not work.我对 jquery 了解不多,但这在我看来是可行的,但仍然不起作用。

Kindly give a simple solution请给出一个简单的解决方案

 $(document).ready(function() { $("td[colspan=2]").hide(); $("tr.sector").click(function() { $(this).next().toggle(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="table-responsive"> <table class="table"> <tr> <td scope="col">Sector</th> <td scope="col">Total Qty</th> </tr> <tr class="sector"> <td>Sector no. 2</td> <td> M : 1, </td> </tr> <tr> <td colspan="2"> <p> <div> <div class="row"> <div class="col-4">Plot no. 5 | NPSC Apartment</div> <div class="col-4">1 Packets</div> <div class="col-4"><a href="/home/mark-deliveries?sector=&amp;plot=5">Details</a></div> </div> </div> </p> </td> </tr> <tr class="sector"> <td>Sector no. 3</td> <td> M : 2, MJ : 1, </td> </tr> <tr> <td colspan="2"> <p> <div> <div class="row"> <div class="col-4">Plot no. 1 | Heritage Tower (Sawan CGHS)</div> <div class="col-4">1 Packets</div> <div class="col-4"><a href="/home/mark-deliveries?sector=&amp;plot=1">Details</a></div> </div> <div class="row"> <div class="col-4">Plot no. 8 | Himachali Apartment</div> <div class="col-4">2 Packets</div> <div class="col-4"><a href="/home/mark-deliveries?sector=&amp;plot=8">Details</a></div> </div> </div> </p> </td> </tr> </table> </div> </div>

You are hiding the td[colspan=2] inside tr and trying to toggle that same tr without actuality showing the td you have hidden in first place.您将td[colspan=2]隐藏在tr并尝试切换相同的tr而实际上不显示您首先隐藏的td

So you need to use next to get next tr sibling and find inside td[colspan=2] you have hidden and toggle that.所以你需要使用next来获取下一个tr兄弟并在td[colspan=2]你已经隐藏并切换它。

Please read documentation on Jquery Tree Traversal and next time watch for your HTML tree.阅读有关 Jquery 树遍历的文档,下次注意您的 HTML 树。 Its all well documented and explained on link provided.它的所有记录都很好,并在提供的链接上进行了解释。

 $(document).ready(function() { $("tr > td[colspan=2]").hide(); $("tr.sector").click(function() { $(this).next().find("td[colspan=2]").toggle(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="table-responsive"> <table class="table"> <tr> <td scope="col">Sector</th> <td scope="col">Total Qty</th> </tr> <tr class="sector"> <td>Sector no. 2</td> <td> M : 1, </td> </tr> <tr> <td colspan="2"> <p> <div> <div class="row"> <div class="col-4">Plot no. 5 | NPSC Apartment</div> <div class="col-4">1 Packets</div> <div class="col-4"><a href="/home/mark-deliveries?sector=&amp;plot=5">Details</a></div> </div> </div> </p> </td> </tr> <tr class="sector"> <td>Sector no. 3</td> <td> M : 2, MJ : 1, </td> </tr> <tr> <td colspan="2"> <p> <div> <div class="row"> <div class="col-4">Plot no. 1 | Heritage Tower (Sawan CGHS)</div> <div class="col-4">1 Packets</div> <div class="col-4"><a href="/home/mark-deliveries?sector=&amp;plot=1">Details</a></div> </div> <div class="row"> <div class="col-4">Plot no. 8 | Himachali Apartment</div> <div class="col-4">2 Packets</div> <div class="col-4"><a href="/home/mark-deliveries?sector=&amp;plot=8">Details</a></div> </div> </div> </p> </td> </tr> </table> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM