Implementation of multiple Select multiple selection _ form effects with JavaScript without CTRL

Source: Internet
Author: User
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <ptml> <pead> <title> New Document &L t;/title> <meta name= "generator" content= "EditPlus" > <meta name= "Author" content= "" > <meta name= "Key Words "content=" "> <meta name=" Description "content=" "> </pead> <body> <select id=" Oselect "NA Me= "Cars" size= "ten" multiple> <option value= "BMW" > BMW </option> <option value= "Porsche" > Porsche </ option> <option value= "Benz" > Mercedes-Benz </option> <option value= "Santana" > Santana </option> <option Value= "LK" > Lincoln </option> <option value= "?" > Logo </option> <option value= "Bus" > Grand public </option> </select> <input type= "button" id= " btnshowselected "value= View checked item index" > </body> <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 () + Separ Ator; } if (sselectedindexes = = "") return the 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> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.