当我尝试将(非重复的)所选内容从一个列表框复制到另一个列表框时,我遇到了一些问题。我一直得到“无法获取属性的值:对象为空或未定义”异常。
以下是我的代码
function CopyItem(from, to) {
var src = document.getElementById(from);
var dest = document.getElementById(to);
for (var i = 0; i < src.options.length; i++) {
if (src.options[i].selected) {
var found = false;
for (var j = 0; j < dest.options.length; j++) {
if (dest.options[j].value == src.option[i].value) {
found = true;
break;
}
}
if (!found) {
var newOption = document.createElement("option");
newOption.text = src.options[i].text;
newOption.value = src.options[i].value;
dest.options[dest.options.length] = newOption;
}
}
}
}
@Html.ListBox("lvDataList", Model.DataList, new { id = "SelectionList", Multiple = "multiple", Size = 15, style = "width: 100%;" })
@Html.ListBoxFor(x => x.SelectedData, Model.SelectedDataList, new { id = "SelectedList", Multiple = "multiple", Size = 15, style = "width: 100%;" })
<input type="button" id="btnAdd" title="Add Selected Events" onclick="CopyItem('SelectionList', 'SelectedList', true)" />
有人可以解释我在哪里编码错误吗? 感谢。
答案 0 :(得分:1)
我猜你应该排队src.options
而不是src.option
if (dest.options[j].value == src.option[i].value) {
答案 1 :(得分:0)
尝试if (dest.options[j].value == src.options[i].value)
代替if (dest.options[j].value == src.option[i].value)