javascript - HTML仅显示具有特定值的表格数据

标签 javascript jquery html

我有一个 html 表,我只想显示那些在与特定表头相对应的表数据中具有特定值的行。例如,您可以假设就像 SQL 查询一样

select * from mytable where name="jone";

代码:

$("#datatable").find("td").each(function() {
  var test = $(this).text();
  alert(test);
});
		<div class="x_content">
                    <table id="datatable" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Name</th>
                          <th>Position</th>
                          <th>Office</th>
                          <th>Age</th>
                          <th>Start date</th>
                          <th>Salary</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <td>Tiger Nixon</td>
                          <td>System Architect</td>
                          <td>Edinburgh</td>
                          <td>61</td>
                          <td>2011/04/25</td>
                          <td>$320,800</td>
                        </tr>
                        <tr>
                          <td>Garrett Winters</td>
                          <td>Accountant</td>
                          <td>Tokyo</td>
                          <td>63</td>
                          <td>2011/07/25</td>
                          <td>$170,750</td>
                        </tr>
                        </tbody>
					</table>
			</div>

最佳答案

Use :contains selector to select elements having specified content

$("#datatable tbody tr").hide();
$("#datatable tr:contains('Edinburgh')").show();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div class="x_content">
  <table id="datatable" class="table table-striped table-bordered">
    <thead>
      <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Age</th>
        <th>Start date</th>
        <th>Salary</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Tiger Nixon</td>
        <td>System Architect</td>
        <td>Edinburgh</td>
        <td>61</td>
        <td>2011/04/25</td>
        <td>$320,800</td>
      </tr>
      <tr>
        <td>Garrett Winters</td>
        <td>Accountant</td>
        <td>Tokyo</td>
        <td>63</td>
        <td>2011/07/25</td>
        <td>$170,750</td>
      </tr>
    </tbody>
  </table>
</div>

关于javascript - HTML仅显示具有特定值的表格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879193/

相关文章:

javascript - Cordova Facebook 插件

javascript - Node.js 和 amqp 的 ETIMEDOUT 问题

javascript - SOAP跨域请求如何使用JQuery

jquery - 鼠标点击时添加点进行映射

javascript - ReactJS - 停止按钮 onClick 从 &lt;input&gt; 获取焦点

javascript - 日期的正则表达式

javascript - 动态添加的 JavaScript 在 IE 中找不到动态添加的字段

jquery - 指定导航栏中所选选项卡的样式

php - 网页正在加载到页面中间位置

javascript - KML 文件上的 OpenLayers 彩色多边形