开发者

"G is undefined" when using javascript object

I'm using a javascript object to populate a dropdown based on a previous input.

My object looks like the following:

var cars = {
        'A.C.' : ["16","16/80","16/90","2 Litre","2-1","3 Litre","428","Ace","Aceba","Cobra","Greyhound","Me","Shelby"],

        'Alfa Romeo' : ["145","146"]
}

Its bigger than that but you get the jist. Everything was working fine until I tried to use the data to populate a dropdown based on the manufacturer. My jQuery code to populate the dropdown looks like this:

function populateModelDropdown(manufacturer) {

    //clear the old dropdown values
    $('#carModel').html("");

    //getthe previous selection if one exists
    var prevSelection = 开发者_StackOverflow中文版$("#carModel_hidden").val();

    $.each(cars[manufacturer], function(val, text) {

        if (text == prevSelection) {
            $('#carModel').append(
                $('<option selected></option>').val(text).html(text));
        }
        else {
            $('#carModel').append(
                $('<option></option>').val(text).html(text));
        }
    });


}

However, as soon as I enter the each loop, I get the error "G is undefined" coming from the file "jquery-tools.js" which is a jquery library which I do not touch.

I see that this problem has occured before but with JSON data. Can anyone point out as to why its occuring for me?

Thanks.


A simple for loop did the job!

function populateModelDropdown(manufacturer) {

    if (manufacturer.length > 0) {

        $('#carModel').html("");

        var prevSelection = $("#carModel_hidden").val();

        for (var i=0; i < cars[manufacturer].length; i++) {

            var model = cars[manufacturer][i];

            if (model == prevSelection) {
                $('#carModel').append(
                    $('<option selected="selected"></option>').val(model).html(model));
            }
            else {
                $('#carModel').append(
                    $('<option></option>').val(model).html(model));
            }
        }

    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜