[英]search table for img using alt attribute and display the whole row
I want to search images from the table using its alt attribute and display the whole row using javascript or jquery.我想使用其 alt 属性从表中搜索图像,并使用 javascript 或 jquery 显示整行。
<div class="input-group">
<span class="input-group-addon">Filter</span>
<input id="filter" type="text" class="form-control" placeholder="Search Airline here">
</div>
<table class="customTable">
<thead>
<tr>
<th>Airline</th>
<th>Authorized Laboratories</th>
<th>Duration for PCR before flight</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="http://www.timestravel.com/images/airlines/EY.png" alt="etihad" width="174" border="0" style="display:block; margin-left: auto; margin-right: auto; width: 70%;" /></td>
<td>
<ul>
<li>Chughtai Lab</li>
<li>Agha Khan Laboratory</li>
<li>Shaukat Khanum</li>
<li>Dr Essa Labroratory</li>
<li>Islamabad Diagnostic Center</li>
<li>Excel Lab</li>
</ul>
</td>
<td>96 Hours</td>
</tr>
what might be its javascript or jquery?它的 javascript 或 jquery 可能是什么?
Try below code.试试下面的代码。
$("#filter").on("keyup", function() { var q = $(this).val(); if(q == '') { $(".customTable").find("tbody tr").removeClass("hidden"); } else { $(".customTable").find("tbody tr img").each(function() { if($(this).attr("alt").indexOf(q) == -1) { $(this).closest('tr').addClass("hidden"); } }); } });
.hidden { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="input-group"> <span class="input-group-addon">Filter</span> <input id="filter" type="text" class="form-control" placeholder="Search Airline here"> </div> <table class="customTable" border="1"> <thead> <tr> <th>Airline</th> <th>Authorized Laboratories</th> <th>Duration for PCR before flight</th> </tr> </thead> <tbody> <tr> <td><img src="http://www.timestravel.com/images/airlines/EY.png" alt="etihad" width="174" border="0" style="display:block; margin-left: auto; margin-right: auto; width: 70%;" /></td> <td> <ul> <li>Chughtai Lab</li> <li>Agha Khan Laboratory</li> <li>Shaukat Khanum</li> <li>Dr Essa Labroratory</li> <li>Islamabad Diagnostic Center</li> <li>Excel Lab</li> </ul> </td> <td>96 Hours</td> </tr> <tr> <td><img src="http://www.timestravel.com/images/airlines/EY.png" alt="Emirates" width="174" border="0" style="display:block; margin-left: auto; margin-right: auto; width: 70%;" /></td> <td> <ul> <li>Chughtai Lab</li> <li>Agha Khan Laboratory</li> <li>Shaukat Khanum</li> <li>Dr Essa Labroratory</li> <li>Islamabad Diagnostic Center</li> <li>Excel Lab</li> </ul> </td> <td>96 Hours</td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.