<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>外部 Js を導入する必要がある場合は、</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>