<div class="htmlarea"> <textarea id="runcode78809"> <title> 새 문서 </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <select id="oselect" name="cars" size="10" multiple> <option value="BMW">BMW</option> <option value="Porsche">포르쉐</option> <option value="Benz">메르세데스-벤츠</option> <option value="Santana">산타나</option> <option value="LK">링컨</option> <option value="?">로고</option> <option value="Bus">대국민</option> </select> <input type="button" id="btnShowSelected" value="查看选中项索引"> </textarea> <br><input onclick="runEx('runcode78809')" type="button" value="运行代码"><input onclick="doCopy('runcode78809')" type="button" value="复制代码"> <input onclick="doSave(runcode78809)" type="button" value="保存代码"> <a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">[Ctrl A 모두 선택 참고: </a>외부 J를 도입해야 하는 경우 실행하려면 새로 고쳐야 합니다 </div>]<script language="JavaScript"> <!-- String.prototype.trimEnd = function(trimString) { var re = new RegExp(trimString+"*$", "g"); return this.replace(re, ""); }; Array.prototype.indexOf = function(itemValue) { var nIndex = -1; for (var i=0; i<this.length; i++) { if (this[i] == itemValue) nIndex = i; } return nIndex; }; var oSpan = document.getElementById("spaOutput"); var oSele = document.getElementById("oSelect"); var oBtn = document.getElementById("btnShowSelected"); oBtn.onclick = function() { alert(getSelectedIndexes(oSele)); }; function getSelectedIndexes(oSele) { var sSelectedIndexes = ""; var separator = ","; for (var i=0; i<oSele.options.length; i++) { if (oSele.options[i].selected) sSelectedIndexes += i.toString() + separator; } if (sSelectedIndexes == "") return new Array(0); else return sSelectedIndexes.trimEnd(separator).split(separator); } oSele.onclick = function() { this.selectedIndexes = getSelectedIndexes(this); // Debug //oSpan.innerHTML += " " + "onclick " + this.selectedIndexes; if (this.selectedIndexes.length == 1) { var nSelectedIndex = parseInt(this.selectedIndexes[0]); if (!isNaN(nSelectedIndex)) this.options[nSelectedIndex].selected = false; } }; oSele.onchange = function() { // Debug //oSpan.innerHTML += " " + "onchange " + this.selectedIndex; var j = 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) { var nSelectedIndex; for (var i=0; i<this.selectedIndexes.length; i++) { nSelectedIndex = parseInt(this.selectedIndexes[i]); this.options[nSelectedIndex].selected = true; } } }; //--> </script>