调整表的jQuery搜索
我有一个表的搜索功能,我想调整它,只在标题列中查找匹配项。并隐藏与标题文本不匹配的所有项目。我的代码 JS HTML调整表的jQuery搜索,jquery,search,html-table,Jquery,Search,Html Table,我有一个表的搜索功能,我想调整它,只在标题列中查找匹配项。并隐藏与标题文本不匹配的所有项目。我的代码 JS HTML Handlingar Title在你的js中尝试一下。希望它能起作用 $(document).ready(function() { var $rows = $('#table tbody tr'); $('#search').keyup(function () { var val = $.trim($(this).val()).rep
Handlingar
Title在你的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();
});
});
你的代码对我来说很好是的,但它会在所有Coulmn上搜索。我想缩小范围,只查找标题栏中的匹配项。我已经修改了jusyvar text=$($(this).find('td')[1]).text().replace(/\s+/g',).toLowerCase()代码>行我自己就这样解决了:var text=$(this).children(“:eq(“+”1“+”)).text().replace(/\s+/g').toLowerCase()代码>你认为巫婆一号更好吗?顺便说一下,这两个都可以,因为只有5个可以搜索。你可以随意使用:)“试试这个”并不能解释什么是不同的或者为什么它有用
<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>
$(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();
});
});