javascript未定义的变量/数组

时间:2012-10-17 17:56:26

标签: javascript jquery arrays

我有一个小函数,它使用带有url的动态文本框列表并将其发送到服务器。我试图将URL加载到一个数组中然后在JSON对象中传递它们。问题是,当我到达代码中的那一点时,我不断收到错误:

TypeError: url_data[this_url] is undefined
http://localhost/2011/admin/public/assets/js/tssol.ui.js
Line 196

以下是返回该错误的块:

$("#dialogEditor").dialog('option', 'buttons', {'Save' : function() {
    /* Define variables */
    var url_data = new Array();
    var this_url = 0;

    /* I've tried this with and without explicitly passing the vars... */
    $("#website_editor input").each(function(url_data, this_url) {
        var id =$(this).attr('name').match(/\d+/);
        var url=$(this).val();

        /* Test the varible each iteration */
        console.log("this_url: " + this_url + " id: " + id + " url: " + url);

    /* Line 196 */ url_data[this_url].id = id;
        url_data[this_url].url = url;

        this_url++;
});


data = JSON.stringify(url_data);
//data = url_data;
//console.log(data);

$.ajax({
    url: 'ajax.websites.php',
    dataType: 'json',
    data: {
        action: "update_resort",
        ResortId: resort,
        data: data
    }
});

$(this).dialog("close");
},
'Cancel': function(){$(this).dialog("close");
}});
},

抱歉格式化

1 个答案:

答案 0 :(得分:3)

我会这样做:

url_data[this_url] = {
    id: id,
    url: url
}