Javascript +防止从列表框中打印出重复值到标签

时间:2011-10-02 15:04:29

标签: javascript asp.net vb.net

我正在尝试阻止将重复值从列表框打印到标签,这是我所做的,但显然它不对,没有错误但是没有打印到我的标签,如果我删除这一行:

if (arSelected.indexOf(selectBox.option[i].selected == -1))

它打印出值,但显然每当我点击并突出显示列表框中的值时,它会再次将它们打印到我的标签上。请善意的建议。谢谢!

<select multiple size="8" style="width: 135px" onBlur="selectAl ('QualMemTypeToBox',true)"  id="QualMemTypeToBox"></select>

function selectAll(selectBox, selectAll) {
        var arSelected = "";
        // have we been passed an ID
        if (typeof selectBox == "string") {
            selectBox = document.getElementById(selectBox);
        }
        // is the select box a multiple select box?
        if (selectBox.type == "select-multiple") {
            for (var i = 0; i < selectBox.options.length; i++) {
                selectBox.options[i].selected = selectAll;

                if (arSelected.indexOf(selectBox.option[i].selected == -1)) {

                    document.getElementById('<%=uilblDestinationQualMemType.ClientID%>').innerHTML += selectBox.options[i].value + " | ";
                    arSelected += selectBox.options[i].selected;
                }
             }
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试:

function selectAll(selectBox, selectAll) {
    var arSelected = "";
    // have we been passed an ID
    if (typeof selectBox == "string") {
        selectBox = document.getElementById(selectBox);
    }
    //reset the label every time the function is called
    document.getElementById('<%=uilblDestinationQualMemType.ClientID%>').innerHTML="";

    // is the select box a multiple select box?
    if (selectBox.type == "select-multiple") {
        for (var i = 0; i < selectBox.options.length; i++) {
            selectBox.options[i].selected = selectAll;
            //make sure you process only items that have been checked
            //AND you need to track the VALUE of the <option>
            //NOT the "selected" attribute
            if (selectBox.options[i].selected && arSelected.indexOf(selectBox.options[i].value) == -1) {

                document.getElementById('<%=uilblDestinationQualMemType.ClientID%>').innerHTML += selectBox.options[i].value + " | ";
                //here is where you actually update the variable with the VALUE
                //of the <option>, not the "selected" attribute
                arSelected += selectBox.options[i].value;
            }
         }
    }
}