5

这是json。我在选择框中显示年份并制作。当我选择一年并使其相关数据应过滤并显示在网格中时。例如,当我选择 2010 年并定义相关数据时,即 2010 年。def, 300 & 5000 应该显示在网格中。任何人都可以在不使用任何 jquery 插件的情况下帮助我做到这一点。

var data = [
    { Year: "2011", Make: "abc", Model: "100", SubModel: "5000", },
    { Year: "2011", Make: "abc", Model: "200", SubModel: "6000",  },
    { Year: "2010", Make: "def", Model: "300", SubModel: "5000",  },
    { Year: "2011", Make: "def", Model: "100", SubModel: "1000",  }
];

这是我的代码:http: //jsfiddle.net/qK2A3/2/

4

3 回答 3

1

回答我的问题

function getRelated() {
        $.each(g_Vehicle, function (index) {
            var sMake = g_Vehicle[index].Make;
            if (g_Vehicle[index].Make == $('#DropDown_Make').val() && g_Vehicle[index].Year == $('#DropDown_Year').val()) {
                $(".ModelClass").html(g_Vehicle[index].Model);
                $(".SubModelClass").html(g_Vehicle[index].SubModel);
            }
        });
    };

演示: http: //jsfiddle.net/ybT7a/ 它正在工作。

于 2012-11-15T07:26:10.103 回答
0

你可以试试这种方式。

HTML 代码:

<select class="target">
  <option value="2010" >2010</option>
  <option value="2011">2011</option>
</select>

JAVASCRIPT代码:

$('.target').change(function() {
  var selected_value = $(this).val();
  jQuery.each(data[0] ,function(key,val){
     if(val.Year == selected_value){
         //code to add to grid go here
     }
  })
});
于 2012-11-12T05:28:14.833 回答
0

您需要遍历对象数组,找到匹配年份的对象并使用该对象解析一些 html。可以通过不同的方式做到这一点,我正在使用数组实用程序方法$.grep来执行数组的循环。请注意,演示必须从发布的数据数组中的对象中删除尾随逗号。IE 讨厌尾随逗号并且会中断。

var year = /*your code to retrieve year*/  2010;/* test value*/
var make= /*your code to retrieve year*/ 'def';/* test value*/

var obj=$.grep( data, function(item, idx){
    return item.Year == year && item.Make == make;
})[0];

/* create some html from the object that matches year and make*/

$('body').append('<p>Model: '+ obj.Model +', SubModel: '+obj.SubModel+'</p>');

演示:http: //jsfiddle.net/uPEQ7/

http://api.jquery.com/jQuery.grep/的 API 参考$.grep

于 2012-11-12T05:48:35.987 回答