当我尝试var a = ar_url2.concat(ar_desc2);
将我的数组合并为一个时,它返回 null。我敢肯定这很简单,但我现在花了几个小时来解决这个问题,并解释为什么会发生这种情况会很好。在下面的代码中,我尝试过while(ar_url2.length)a.push(ar_url2.shift());
,它返回相同的 null ...
function agregar() {
var i = 0,
textarea;
var ar_desc = [];
while (textarea = document.getElementsByTagName('textarea')[i++]) {
if (textarea.id.match(/^desc_([0-9]+)$/)) {
ar_desc.push(textarea.id);
}
}
var desc_count_demo = document.getElementById('desc_count').value;
var desc_count = desc_count_demo - 1;
i = 0;
var ar_desc2 = [];
var campo = null;
while (i <= desc_count) {
campo = document.getElementById(ar_desc[i]).value;
ar_desc2[ar_desc[i]] = campo;
i++;
}
i = 0;
var input;
var ar_url = [];
while (input = document.getElementsByTagName('input')[i++]) {
if (input.id.match(/^url_([0-9]+)$/)) {
ar_url.push(input.id);
}
}
var url_count_demo2 = document.getElementById('url_count').value;
var url_count2 = url_count_demo2 - 1;
i = 0;
var ar_url2 = [];
while (i <= url_count2) {
campo = document.getElementById(ar_url[i]).value;
ar_url2[ar_url[i]] = campo;
i++;
}
// var a = Array.prototype.concat.call(ar_url2, ar_desc2);
while (ar_url2.length) a.push(ar_url2.shift());
function url(data) {
var ret = [];
for (var d in data)
ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
return ret.join("&");
}
window.open('alta1.php?'+url(a));
}
编辑:如果我传递给函数 url(ar_url2) 或 url(ar_desc2),则 URL 中的返回值为
http://localhost/proj1/alta1.php?url_0=inpit&url_1=input
和
http://localhost/proj1/alta1.php?desc_0=input&desc_1=input
但仍然无法将两者合二为一...