【发布时间】:2016-05-09 05:28:49
【问题描述】:
请看下面的jsfiddle。
https://jsfiddle.net/51Le6o06/42/
如您所见,表格过滤器 ('.filter-gift') 使用其下方 HTML 表格中的数据填充自身。我隐藏了所有其他脚本,以便更容易看到。
问题是当我选择一个过滤器(例如“免费电视”)时,相应的表格过滤器正确,但如果我在表格中选择默认过滤器选项,过滤器会隐藏所有行。
理想情况下,选择默认选项“-Select-”应该显示所有行,我该如何更改我的代码,所以我的函数就是这种情况。
使用 jQuery/Javascript:
$(document).ready(function() {
$('.filter-gift').each(filterItems);
});
function filterItems(e) {
var items = [];
var table = '';
tableId = $(this).parent().parent().attr('tag')
var listItems = "";
listItems += "<option value='0'> -Select- </option>";
$('div[tag="' + tableId + '"] table.internalActivities .information').each(function (i) {
var itm = $(this)[0].innerText;
if ($.inArray(itm, items) == -1) {
items.push($(this)[0].innerText);
listItems += "<option value='" + i + "'>" + $(this)[0].innerText + "</option>";
}
});
$('div[tag="' + tableId+ '"] .filter-gift').html(listItems);
$('.filter-gift').change(function () {
var tableIdC = $(this).parent().parent().attr('tag');
var text = $('div[tag="' + tableIdC + '"] select option:selected')[0].text.replace(/(\r\n|\n|\r| |)/gm, "");;
$('div[tag="' + tableIdC + '"] .product-information-row').each(function (i) {
if ($(this).text().replace(/(\r\n|\n|\r| |)/gm, "") == text) {
$(this).show();
$(this).prev().show();
$(this).next().show();
}
else {
$(this).hide();
$(this).prev().hide();
$(this).next().hide();
}
});
});
}
【问题讨论】:
标签: javascript jquery html drop-down-menu