哇哦,这么小任务的代码太多了...
无论如何,在您的每个“更改功能”中,您都在重新加载其他过滤器选择。这就是重置选择框的原因
例如:
fxnSelectMake = function () {
$('#tableID tr').remove();
g_newRows = "";
$.each(g_Vehicle, function (index) {
if (g_Vehicle[index].Make == $('#DropDown_Make').val()) {
fxnCreateRow(g_Vehicle, index);
}
});
$('#tableID').append(g_newRows);
fxnReLoadingModelFilters(); // <-- this is what i mean
fxnReLoadingSubModelFilters(); // <-- and this
fxnReLoadingYearFilters(); // <-- and this
},
但这只是第一部分。你的主要问题是这段代码:
$.each(g_Vehicle, function (index) {
if (g_Vehicle[index].Make == $('#DropDown_Make').val()) {
fxnCreateRow(g_Vehicle, index);
}
});
您的 g_Vehicle-Object 仍然是这里的 TOTAL 对象,您只是在检查当前选择的值(不是年份等)
我编写了一个函数“isInIndex”,它检查所有 4 个当前选择的值,并且仅在当前车辆行对所有 4 个选择框都有效时才返回 true:
isInIndex = function(vehicle) {
return ($("#DropDown_Year").prop("selectedIndex") === 0 || vehicle.Year === $('#DropDown_Year').val()) &&
($("#DropDown_Make").prop("selectedIndex") === 0 || vehicle.Make === $('#DropDown_Make').val()) &&
($("#DropDown_Model").prop("selectedIndex") === 0 || vehicle.Model === $('#DropDown_Model').val()) &&
($("#DropDown_SubModel").prop("selectedIndex") === 0 || vehicle.SubModel === $('#DropDown_SubModel').val())
}
然后我在您的每个“更改函数”中调用此函数:
$.each(g_Vehicle, function (index) {
if (isInIndex(g_Vehicle[index])) {
fxnCreateRow(g_Vehicle, index);
}
});
这里更新和工作的小提琴:http://jsfiddle.net/pW6P6/10/
编辑:您的代码中可能有很多优化可能性,其中之一是您应该将 jQuery-Objects 保存到变量中,并使用它们:
例如:
var $dropDownMake = $('#DropDown_Make');
// and later
$dropDownMake.val()