我创建了一个名为memberList的var(在任何方法之外)。它包含选择字段的....项目。我这样构建它:
//build the option list, append
for (var i = 0; i < jsonarray.length; i++) {
var obj = jsonarray[i];
daySelect = document.getElementById('member');
daySelect.options[daySelect.options.length] = new Option(obj.lastName + ', ' + obj.firstName, obj.memberId);
console.log(obj.lastName + ', ' + obj.firstName, obj.memberId);
}
memberList = daySelect.options;
然后在我的表单上,我有能力让人们添加更多选择框(本质上是更多人)。然后我把这个代码放在一起,创建新的选择框就好了,但我似乎无法让数组继续进行。只要有人想要在表单中添加新行,就会调用此方法。
var selectMember = document.createElement("select");
selectMember.style = "width:200px;";
selectMember.id = "member" + document.getElementsByName('member').length;
selectMember.name = "member";
selectMember.className = "form-control";
//selectMember.options = memberList;
console.log('memberlist-a: ' + memberList.length);
var cloneList = jQuery.extend({}, memberList);
console.log('memberlist-b: ' + memberList.length);
for (var i = 0; i < cloneList.length; i++) {
var obj = cloneList[i];
selectMember.options[selectMember.options.length] = obj;
console.log('memberlist-inloop: ' + memberList.length);
}
console.log('memberlist: ' + memberList.length);
我的控制台日志告诉我:
我的问题:
1)我如何遍历array2(clonelist)从array1(memberList)中删除项目的副本(是的,我已经尝试过深层复制)?
2)无论如何,为什么迭代和添加项目到html选择选项会使它们从数组中消失?