简体   繁体   English

在选择框jquery中选择选项和过滤器值

[英]Selecting options and filter values in select box jquery

My problem is when I select an option in anyone of the select box and then selecting another option in other Select box, the values are automatically changing. 我的问题是,当我在任何选择框中选择一个选项,然后在其他选择框中选择另一个选项时,值会自动更改。 How can I solve this. 我该怎么解决这个问题。 I want to filter values according to my selection and display it in a table without using plugins. 我想根据我的选择过滤值,并在不使用插件的情况下将其显示在表中。

Here is my code:

http://jsfiddle.net/pW6P6/4/ http://jsfiddle.net/pW6P6/4/

I combined your sorting functions in to one function. 我将您的排序功能合并到一个功能中。

ie, fxnSelectYear, fxnSelectMake, fxnSelectModel, fxnSelectSubModel to fxnUpdateGrid 即,fxnSelectYear,fxnSelectMake,fxnSelectModel,fxnSelectSubModel到fxnUpdateGrid

It will be better if you can combine your fxnReLoading.. functions to one . 如果你可以将fxnReLoading ..函数合并到一个函数中会更好。

DEMO DEMO

Hope this helps. 希望这可以帮助。 Feel free to ask me questions. 随意问我问题。

If you want to conserve your logic. 如果你想保存你的逻辑。 You can save the selected value before deleting each options and select it after you reiitialized your select values : 您可以在删除每个选项之前保存所选值,并在重新初始化选择值后选择它:

http://jsfiddle.net/pW6P6/9/ http://jsfiddle.net/pW6P6/9/

var selected = $('#DropDown_Year option:selected').val();
$('#DropDown_Year option[value]').remove();
iHtmlYear = fxnOptions(g_YearsArray);
$('#DropDown_Year').append(iHtmlYear);
$("#DropDown_Year option[value=" + selected + "]").attr("selected", true);

whooo, thats a hell lot of code for such a small task... 哎呀,这对于这么小的任务来说就是很多代码......

anyways, in each of your "change-functions" you are reloading your other filter-selects. 无论如何,在你的每个“更改函数”中,你正在重新加载其他过滤选择。 thats why the select boxes are reset 这就是重置选择框的原因

eg: 例如:

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

},

but this is only part one. 但这只是第一部分。 your main problem is this piece of code: 你的主要问题是这段代码:

$.each(g_Vehicle, function (index) {
  if (g_Vehicle[index].Make == $('#DropDown_Make').val()) {
    fxnCreateRow(g_Vehicle, index);
  }
});

your g_Vehicle-Object is still the TOTAL object here, and you are just checking for the current selected value (not for year and so on) 你的g_Vehicle-Object在这里仍然是TOTAL对象,你只是检查当前选中的值(不是年份等)

i have written a function "isInIndex" which checks all 4 current selected values, and only returns true, if the current vehicle row is valid for all 4 select boxes: 我编写了一个函数“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())
}

then i call this function in each of your "change-functions": 然后我在每个“变更函数”中调用此函数:

$.each(g_Vehicle, function (index) {
  if (isInIndex(g_Vehicle[index])) {
    fxnCreateRow(g_Vehicle, index);
  }
});

the updated and working fiddle here: http://jsfiddle.net/pW6P6/10/ 这里更新和工作的小提琴: http//jsfiddle.net/pW6P6/10/

edit: there are probably a lot of optimization-possibilities in your code, one of them is that you should save your jQuery-Objects into variables, and work with them: 编辑:您的代码中可能存在许多优化可能性,其中之一是您应该将jQuery-Objects保存到变量中,并使用它们:

for example: 例如:

var $dropDownMake = $('#DropDown_Make');

// and later
$dropDownMake.val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM