如何筛选多个选中的

时间:2017-09-08 00:36:16

标签: javascript jquery

我有一个听取选择元素的函数。

function initEventListeners () {

     // Listening to category select
     $categorySelect.on("change", function () {
        helper.createFilteredSamples();
     });

     // Listening to platform select
     $platformSelect.on("change", function () {
        helper.createFilteredSamples();
     });

     // Listening to language select
     $langugeSelect.on("change", function () {
        helper.createFilteredSamples();
     });
}

用于返回匹配样本的辅助方法

var helper = {
     // create filtered samples based on the search and dropdown values
     createFilteredSamples: function () {
        var categoryValue = $categorySelect.val();
        var languageValue = $langugeSelect.val();
        var platformValue = $platformSelect.val();

        if (categoryValue === "default" && languageValue === "default" && platformValue === "default") {
           helper.createTableUi(samples);
           return;
        }

        var sampleSet = samples.filter(function (sample) {
           var cats = sample.Category.split(" ");
           var langs = sample.Language.split(" ");
           var plats = sample.Platform.split(" ");

           return ($.inArray(categoryValue, cats) !== -1 && $.inArray(languageValue, langs) !== -1 && $.inArray(platformValue, plats) !== -1);
        });

        helper.createTableUi(sampleSet);
     }
  };

问题在于这一行return ($.inArray(categoryValue, cats) !== -1 && $.inArray(languageValue, langs) !== -1 && $.inArray(platformValue, plats) !== -1);。这仅在用户选择了所有三个下拉列表时才有效。如何以用户选择的任何组合获得匹配结果?

0 个答案:

没有答案