jquery - 调整 jQuery 表搜索

标签 jquery search html-table

我有一个表格搜索功能,我想调整它以仅在“标题”列中查找匹配项。并隐藏所有与标题文本不匹配的项目。我的代码

JS

var $rows = $('#table tbody 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();
});

HTML

         <table id="table" class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>Handlingar</th>
                    <th>Title</th>  <-----This column----
                    <th>Pris i kr</th>
                    <th>Skapad</th>
                    <th>Kategori</th>
                </tr>
            </thead>
            <tbody>
                @foreach (BuyAndSellAppWeb.Models.Advertisment objProduct in Model)
                {
                    <tr>
                        @if (objProduct.SellerToken)
                        {
                            <td>

                                @Html.ActionLink("Ändra", "Edit", new { id = objProduct.ID }) | @Html.ActionLink("Radera", "DeleteItem", new { id = objProduct.ID }) | @Html.ActionLink("Detaljer", "Details", new { id = objProduct.ID })

                        </td>
                        }
                        else
                        {
                            <td>
                                @Html.ActionLink("Detaljer", "Details", new { id = objProduct.ID })
                            </td>
                        }
                        <td>@objProduct.ProductTitle</td>
                        <td>@objProduct.Price kr</td>
                        <td>@objProduct.Created.ToString("yyyy/MMM/dd")</td>
                        <td id="category">@objProduct.Category</td>
                    </tr>
                }
            </tbody>
        </table>

尝试了很多不同的组合,但都没有成功。

最佳答案

在你的 js 中试试这个。希望它有效

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

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

关于jquery - 调整 jQuery 表搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46972810/

相关文章:

javascript - 图片上传时调用表单提交的js函数

search - 如何根据 ElasticSearch 中的项目数对搜索结果进行排序?

CSS 宽度和最大宽度组合

html - box-sizing : border-box causing div with padding inside td to have margins, 为什么?

javascript - jquery - 禁用所有选择框?

javascript - 如何计算印度格式的 2 个日期差异

javascript - 使用 jQuery 从 DOM 元素中提取属性

java - 在 java arraylist 中搜索正则表达式

algorithm - 计算对象树中的最大重复子树

jquery - 更改下拉列表中的表行选择不起作用