var g_Year = [
         { Year: "2011" },
         { Year: "2011" },
         { Year: "2010" },
         { Year: "2012" },
         { Year: "2011" },
         { Year: "2012" }
];
fxnDrop = function () {
        $.each(g_Year , function (index, Ele) {
            var iYear = g_Year [index].Year,
                sHtmlYear = '<option value="' + iYear + '">' + iYear + '</option>';
            $("#DropDown_Year").each(function (index, Ele) {
                $("#DropDown_Year").append(sHtmlYear);
                if (Ele.value != iYear) {
                    $("#DropDown_Year").remove(sHtmlYear);
                }
            });
        });
}

在这里我有多年,我的目标是在下拉框中仅显示特定年份。例如:2011在下拉框中仅显示一次。谁能帮我做到这一点。我在保管箱中获得多个2011年值。

最佳答案

试试这个代码

var fxnDrop = function() {
    var yearsArray = [];
    $.each(g_Year, function(index) {
        if($.inArray(+g_Year[index].Year , yearsArray) == -1){
            yearsArray.push(+g_Year[index].Year);
        }
    });
    yearsArray.sort();
    var sHtmlYear = '';
    $.each(yearsArray , function(i){
        sHtmlYear += '<option value="'+ yearsArray[i] +'">'
                         + yearsArray[i]  + '</option>';
    });
     $("#DropDown_Year").append(sHtmlYear);
};

fxnDrop(); ​

Check Fiddle

关于javascript - 在下拉框中插入json值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13284140/

10-09 20:02