【问题标题】:Filter Table Row in Html在 Html 中过滤表行
【发布时间】:2019-09-10 16:50:39
【问题描述】:

我创建了一个 html 页面,其中包含搜索文本和下面的表格,表格中包含一些数据。

我使用了 JSFiddle 上提供的代码。

但它没有工作。请提出类似于所示示例的建议。

我正在使用简单的 html、CSS 和 Javascript 编码。

在这个jQuery中使用:

var $rows = $('#table td');
$('#search').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

    $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
    }).hide();
});

由于这是一个混合应用程序,我可以提供一些更简单的东西吗?

我正在使用以下代码。请检查

<!DOCTYPE html>
<html>
<head>
<title>Search Box Example 2 - default placeholder text gets cleared on click</title>
<link rel="stylesheet" href="css/SalesTable.css">

<meta name="ROBOTS" content="NOINDEX, NOFOLLOW" />
<!-- JAVASCRIPT to clear search text when the field is clicked -->

<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript">

var $rows = $('#table tr');
$('#tfq').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

    $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
    }).hide();
});

</script>



<!-- CSS styles for standard search box with placeholder text-->

</head>
<body>
    <!-- HTML for SEARCH BAR -->
    <div id="tfheader">
        <form id="tfnewsearch" method="get" action="http://www.google.com">
                <input type="text" id="tfq" class="tftextinput2" name="q" size="21" maxlength="120" placeholder="Type to search"><input type="button" value="Search" class="tfbutton2">
        </form>
        <div class="tfclear"></div>
    </div>

    <br>
    <br>

    <div id="tableSalesOrder">
    <table id="table">


    <tr>
    <th>
    A
    </th>
    <th>
    B
    </th>
    <th>
    C
    </th>
    <th>
    D
    </th>
    </tr>

    <script language="javascript">
    <!--

    for(i =0 ;i<4 ; i++)
    {
    document.write('<tr>');
    document.write('<td>12345</td>')
    document.write('<td>34566</td>')
    document.write('<td>345356</td>')
    document.write('<td>Tyjhue</td>')
    document.write('<td><form id="approveForm"><input type = "submit" value="ButtonA"></form></td>')
    document.write('<td><form id="rejectForm"><input type = "submit" value="ButtonB"></form></td>')
    document.write('<td><form id="detailForm"><input type = "submit" value="ButtonC"></form></td>')
    document.write('</tr>')

    }

    //-->
    </script>

    </table>

    </div>

</body>
</html>

【问题讨论】:

标签: javascript jquery css html-table


【解决方案1】:

包括 jQuery

    <head>
     <style type="text/css">
       body {padding: 20px;}
       input {margin-bottom: 5px; padding: 2px 3px; width: 209px;}
       td {padding: 4px; border: 1px #CCC solid; width: 100px;}
     </style>    
    </head>
   <body>
    <input type="text" id="search" placeholder="Type to search">
      <table id="table">
      <tr> <td>Apple</td>
           <td>Green</td>
      </tr>
      <tr> <td>Grapes</td>
           <td>Green</td>
      </tr>
      <tr> <td>Orange</td>
           <td>Orange</td>
      </tr>
   </table>
   <script src="http://code.jquery.com/jquery-2.1.0.js"></script>
   <script type="text/javascript">
     var $rows = $('#table tr');
     $('#search').keyup(function() {
         var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

         $rows.show().filter(function() {
             var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
             return !~text.indexOf(val);
         }).hide();
     });
    </script>
    </body>    

运行代码,

【讨论】:

  • 这个东西可以工作......但请检查我的代码也
  • 在body结束标签前加入Jquery会提高性能。在小提琴中,它包含在外部。
  • 不错。正是我需要的。谢谢!
  • 完美运行。谢谢
【解决方案2】:

应该在 DOM 准备好之后解释 JavaScript 代码。

由于您使用的是&lt;head&gt; 部分中的脚本,#table tr 元素还没有准备好;因此 jQuery 无法选择元素。

您可以使用.ready() 方法检查document 是否准备就绪:

$(document).ready(function() {
    var $rows = $('#table tr');
    $('#search').keyup(function() {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

        $rows.show().filter(function() {
            var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
            return !~text.indexOf(val);
        }).hide();
    });
});

或者把你的&lt;script&gt;...&lt;/script&gt;标签放在底部,就在&lt;/body&gt;结束标签之前。

更多信息:http://learn.jquery.com/using-jquery-core/document-ready/

【讨论】:

    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    相关资源
    最近更新 更多