1
我有一个小函数,它将带有URL的动态文本框列表发送给服务器。我正试图加载到一个数组的url,然后在JSON对象中传递它们。问题是,当我到这一点在代码中,我不断收到错误:javascript未定义的变量/数组
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");
}});
},
很抱歉的格式
什么是你想获得? 'this_url'是'.each'中的输入元素,所以你不能用它作为数组中的键。 – pimvdb
如果'url_data [this_url]'还不存在,则不能在其上设置'id'属性......必须先将一个对象分配给'url_data [this_url]'。 –