0

当我尝试将(非重复的)选定内容从一个列表框复制到另一个列表框时,我遇到了一些问题。我不断收到“无法获取属性值:对象为空或未定义”异常。

以下是我的代码

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)" />

有人可以解释我在哪里编码错误吗?谢谢。

4

2 回答 2

1

我想你应该有src.options而不是src.option排队

 if (dest.options[j].value == src.option[i].value) {
于 2012-10-23T08:21:31.383 回答
0

尝试if (dest.options[j].value == src.options[i].value)

反而if (dest.options[j].value == src.option[i].value)

于 2012-10-23T08:23:18.077 回答