简体   繁体   中英

Filter json with select box option value using jquery

How can i filter the "Make" values according to the year. For eg: When i select "Year" as "2012" the related data ie; only "def" should be displayed in the drop down box. Another problem is the previous data in the table was shown again when i select next values. See the demo for better understanding.

I tried this.

$.each(g_Vehicle, function(index) {
 var sMake = g_Vehicle[index].Make;
  if($('#DropDown_Year').val())=='2011'){  /* for testing purpose i give year as 2011*/
   if ($.inArray(sMake, g_MakesArray) == -1) {
    g_MakesArray.push(sMake);
        } 
 }
});

DEMO

要过滤集合/数组,请使用$ .grep,了解更多相关信息,请参阅jqfaq.com文章,这将详细解释。

function filter() {
    switch($(this).text()) {
    case "2012":
        //filter logic
        $("#dropdown2").html("<select>....</select><select>...</select>");
        break;
    ....
    }
}

$("#dropdown").change(filter);

You can make the second drop box empty when start and fill it with appropriate selects after someone selects year.

I would use grep for this

eg

var newArray = jQuery.grep(g_Vehicle , function(element, index){
  return element.Make == "abc" ; 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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