我有一种情况需要根据条件将某些元素添加到多维数组中。这意味着,元素的类型将基于条件。
if(type == 'text-box'){
var title = jQuery("#textbox-title-"+id).val();
var desc = jQuery("#textbox-desc-"+id).val();
eleDetails[id]['content'] = {title:title, content:desc};
}
else if(type == 'image-box'){
var title = jQuery("#imagebox-title-"+id).val();
var imgurl = jQuery("#imagebox-url-"+id).val();
var desc = jQuery("#imagebox-desc-"+id).val();
eleDetails[id]['content'] = {title:title, imgurl:imgurl, content:desc};
}
那么,这样做的方式是什么?
答案 0 :(得分:0)
某处:
var eleDetails = {};
并在你的循环中:
var title = jQuery("#textbox-title-"+id).val();
var desc = jQuery("#textbox-desc-"+id).val();
// check if eleDetails[id] already exists
if (!eleDetails[id]) {eleDetails[id] = {};}
if(type == 'text-box'){
eleDetails[id]['content'] = {title:title, content:desc};
}
else if(type == 'image-box'){
var imgurl = jQuery("#imagebox-url-"+id).val();
eleDetails[id]['content'] = {title:title, imgurl:imgurl, content:desc};
}