Javascript + Prevent printing out duplicate values from listbox to label
I am trying to prevent printing duplicated values from a listbox to a label, this is what I did but apparently it's not right, there's not errors but nothing is printed out to my label, if I remove this line:
if (arSelected.indexOf(selectBox.option[i].selected == -1))
it prints out the values but apparently everytime I click and highlight on the values in the listbox, it's going to print them out again to my label. Please kindly advice. Thanks!
<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;
}
}
}
}
try:
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;
}
}
}
}
精彩评论