最后想办法,另设计文本框的服务端控件(txtArrCategoryID),用来保存已经添加的分类ID号,当然你不想看到这个控件,就把它的width和height设置为0就行了,不要设置Visible属性设置为False,否则JS程序是认不出来的。
在添加程序中增加一行
document.Form1.txtArrCategoryID.value += selOpt.value + "|";
大删除程序中也添加
document.Form1.txtArrCategoryID.value = ("|" + document.Form1.txtArrCategoryID.value).replace("|" + opt.value + "|", "|");
document.Form1.txtArrCategoryID.value = document.Form1.txtArrCategoryID.value.substring(1, document.Form1.txtArrCategoryID.value.length)
完整JS程序
<script language="javascript" src="../Script/function.js"></script>
<script language="javascript">
function cleanOptionString(str)
{
str = str.replace("├", "");
str = str.replace("│", "");
str = str.replace("└", "");
str = str.replace(" ", "");
str = Trim(str);
return str;
}
function addOption()
{
if (document.Form1.lstCategory.selectedIndex < 0) return;
opt = document.Form1.lstCategory.options[document.Form1.lstCategory.selectedIndex];
selOpt = new Option(cleanOptionString(opt.text), opt.value);
if(document.Form1.lstSelectCategory.length > 0)
{
for (i = 0; i < document.Form1.lstSelectCategory.length; i++)
{
if (document.Form1.lstSelectCategory.options[i].value == selOpt.value)
{
return;
}
}
}
document.Form1.lstSelectCategory.options.add(selOpt);
document.Form1.txtArrCategoryID.value += selOpt.value + "|";
}
function removeOption()
{
if (document.Form1.lstSelectCategory.selectedIndex < 0) return;
opt = document.Form1.lstSelectCategory.options[document.Form1.lstSelectCategory.selectedIndex];
document.Form1.txtArrCategoryID.value = ("|" + document.Form1.txtArrCategoryID.value).replace("|" + opt.value + "|", "|");
document.Form1.txtArrCategoryID.value = document.Form1.txtArrCategoryID.value.substring(1, document.Form1.txtArrCategoryID.value.length)
document.Form1.lstSelectCategory.options.remove(document.Form1.lstSelectCategory.selectedIndex); }
</script>