<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<html>
<head>
<title>NewDocument</title>
<metaname="Generator"content="EditPlus">
<metaname="Author"content="">
<metaname="Keywords"content="">
<metaname="Description"content="">
</head>
<body>
<selectid="oselect"name="cars"size="10"multiple>
<optionvalue="BMW">宝马</option>
<optionvalue="Porsche">保时捷</option>
<optionvalue="Benz">奔驰</option>
<optionvalue="Santana">桑塔纳</option>
<optionvalue="LK">林肯</option>
<optionvalue="?">标志</option>
<optionvalue="Bus">大公共</option>
</select>
<inputtype="button"id="btnShowSelected"value="查看选中项索引">
</body>
<scriptlanguage="JavaScript">
<!--
String.prototype.trimEnd=function(trimString){
varre=newRegExp(trimString+"*$","g");
returnthis.replace(re,"");
};
Array.prototype.indexOf=function(itemValue){
varnIndex=-1;
for(vari=0;i<this.length;i++)
{
if(this[i]==itemValue)
nIndex=i;
}
returnnIndex;
};
varoSpan=document.getElementById("spaOutput");
varoSele=document.getElementById("oSelect");
varoBtn=document.getElementById("btnShowSelected");
oBtn.onclick=function(){
alert(getSelectedIndexes(oSele));
};
functiongetSelectedIndexes(oSele)
{
varsSelectedIndexes="";
varseparator=",";
for(vari=0;i<oSele.options.length;i++)
{
if(oSele.options[i].selected)
sSelectedIndexes+=i.toString()+separator;
}
if(sSelectedIndexes=="")
returnnewArray(0);
else
returnsSelectedIndexes.trimEnd(separator).split(separator);
}
oSele.onclick=function(){
this.selectedIndexes=getSelectedIndexes(this);
//Debug
//oSpan.innerHTML+="<br/>"+"onclick"+this.selectedIndexes;
if(this.selectedIndexes.length==1)
{
varnSelectedIndex=parseInt(this.selectedIndexes[0]);
if(!isNaN(nSelectedIndex))
this.options[nSelectedIndex].selected=false;
}
};
oSele.onchange=function(){
//Debug
//oSpan.innerHTML+="<br/>"+"onchange"+this.selectedIndex;
varj=this.selectedIndexes.indexOf(this.selectedIndex.toString());
if(j>-1)
{
this.options[this.selectedIndex].selected=false;
this.selectedIndexes.splice(j,1);
}
if(this.selectedIndexes.length>0)
{
varnSelectedIndex;
for(vari=0;i<this.selectedIndexes.length;i++)
{
nSelectedIndex=parseInt(this.selectedIndexes[i]);
this.options[nSelectedIndex].selected=true;
}
}
};
//-->
</script>
</html>
<html>
<head>
<title>NewDocument</title>
<metaname="Generator"content="EditPlus">
<metaname="Author"content="">
<metaname="Keywords"content="">
<metaname="Description"content="">
</head>
<body>
<selectid="oselect"name="cars"size="10"multiple>
<optionvalue="BMW">宝马</option>
<optionvalue="Porsche">保时捷</option>
<optionvalue="Benz">奔驰</option>
<optionvalue="Santana">桑塔纳</option>
<optionvalue="LK">林肯</option>
<optionvalue="?">标志</option>
<optionvalue="Bus">大公共</option>
</select>
<inputtype="button"id="btnShowSelected"value="查看选中项索引">
</body>
<scriptlanguage="JavaScript">
<!--
String.prototype.trimEnd=function(trimString){
varre=newRegExp(trimString+"*$","g");
returnthis.replace(re,"");
};
Array.prototype.indexOf=function(itemValue){
varnIndex=-1;
for(vari=0;i<this.length;i++)
{
if(this[i]==itemValue)
nIndex=i;
}
returnnIndex;
};
varoSpan=document.getElementById("spaOutput");
varoSele=document.getElementById("oSelect");
varoBtn=document.getElementById("btnShowSelected");
oBtn.onclick=function(){
alert(getSelectedIndexes(oSele));
};
functiongetSelectedIndexes(oSele)
{
varsSelectedIndexes="";
varseparator=",";
for(vari=0;i<oSele.options.length;i++)
{
if(oSele.options[i].selected)
sSelectedIndexes+=i.toString()+separator;
}
if(sSelectedIndexes=="")
returnnewArray(0);
else
returnsSelectedIndexes.trimEnd(separator).split(separator);
}
oSele.onclick=function(){
this.selectedIndexes=getSelectedIndexes(this);
//Debug
//oSpan.innerHTML+="<br/>"+"onclick"+this.selectedIndexes;
if(this.selectedIndexes.length==1)
{
varnSelectedIndex=parseInt(this.selectedIndexes[0]);
if(!isNaN(nSelectedIndex))
this.options[nSelectedIndex].selected=false;
}
};
oSele.onchange=function(){
//Debug
//oSpan.innerHTML+="<br/>"+"onchange"+this.selectedIndex;
varj=this.selectedIndexes.indexOf(this.selectedIndex.toString());
if(j>-1)
{
this.options[this.selectedIndex].selected=false;
this.selectedIndexes.splice(j,1);
}
if(this.selectedIndexes.length>0)
{
varnSelectedIndex;
for(vari=0;i<this.selectedIndexes.length;i++)
{
nSelectedIndex=parseInt(this.selectedIndexes[i]);
this.options[nSelectedIndex].selected=true;
}
}
};
//-->
</script>
</html>