我想用3个字段过滤我的表但是这三个都是可选的我可以用一个字段或两个或三个字段进行搜索。至少应设置一个字段。
这就是我所达到的,这段代码对于一个字段完全正常,如果我以这种方式添加其他字段,它们应该被设置。我如何介绍可选性?
$('#submitButton').click(function() {
var quantity = $("[name='quantity']").val();
var category = $('select.categorylist option:selected').val();
var brand = $("[name='brand']").val();
$("table tbody tr").hide();
$("table tbody tr").each(function()
{
if ($(this).find("td:eq(3):contains('" + quantity + "')").html() != null)
{
$(this).show();
}
});
return false ;
});
答案 0 :(得分:1)
您可以使用filter
这样的功能:
$('#submitButton').click(function() {
var quantity = $("[name='quantity']").val(),
category = $('select.categorylist option:selected').val(),
brand = $("[name='brand']").val();
$("table tbody tr:not(.header, .filters)").hide().filter(function() {
var q = quantity ? this.cells[0].innerHTML === quantity : true,
c = category ? this.cells[1].innerHTML === category : true,
b = brand ? this.cells[2].innerHTML === brand : true;
return q && c && b;
}).show();
return false;
});
因此,只有当每列的值与相应的过滤器匹配时,才会显示行。