Use JavaScript to achieve a ListBox left-right selection, single selection, multiple selection, all please _javascript skills

Source: Internet
Author: User

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>list Test </title>
<body>
<div style= "FONT-SIZE:10PT;" >
Note 1: Move left and right to select
<br/>
<br/>
Note: This page is only tested under ie6/firefox1.5. Other browsers or other versions have not been tested.
<br/>
</div>
<form name= "frm" >
<table>
<tr>
<td>
<select name= "Srcselect" size= "6" style= "FONT-SIZE:11PT; width:160px; height:160px "
Multiple= "multiple" ondblclick= "Moveleftorright (document.frm.srcselect,document.frm.objselect)" >
<option value= "1" > Lecturer </option>
</select>
</td>
&LT;TD width= "30px" >
<input align= "left" type= "button" value= ">" onclick= "Moveleftorright (Document.frm.SrcSelect, Document.frm.ObjSelect) ">
<br>
<br>
<input align= "left" type= "button" value= "<" onclick= "Moveleftorright (Document.frm.ObjSelect, Document.frm.SrcSelect) ">
</td>
<td>
<select name= "Objselect" size= "6" style= "FONT-SIZE:11PT; width:160px; height:160px "
Multiple= "multiple" ondblclick= "Moveleftorright (document.frm.objselect,document.frm.srcselect)" >
<option value= "2" > Teaching Administrator </option>
<option value= "3" > Super admin </option>
</select>
</td>
</tr>
</table>
</form>
</body>
<script type= "Text/javascript" language= "JavaScript" >
Move Up
function MoveUp () {
var theobjoptions = document.frm.ObjSelect.options;
for (var i = 1; i < theobjoptions.length; i++) {
if (theobjoptions[i].selected &&!theobjoptions[i-1].selected) {
Swapoptionproperties (Theobjoptions[i], theobjoptions[i-1]);
}
}
}


Move Down
function MoveDown () {
var theobjoptions = document.frm.ObjSelect.options;
for (var i = theobjoptions.length-2 i > 1; i--) {
if (theobjoptions[i].selected &&!theobjoptions[i + 1].selected) {
Swapoptionproperties (Theobjoptions[i], theobjoptions[i + 1]);
}
}
}


    function swapoptionproperties (option1, option2) {
         var tempstr = Option1.value;
        option1.value = option2.value;
        option1.value = tempstr;
        tempstr = Option1.text;
        option1.text = Option2.text;
        option2.text = tempstr;
        tempstr = option1.selected;
        option1.selected = option2.selected;
        option2.selected = tempstr;
   }


The position of the list box moves
function Moveleftorright (fromobj, toobj) {
for (var i = 0; i < fromobj.length; i++) {
var srcoption = fromobj.options[i];
if (srcoption.selected) {
Toobj.appendchild (srcoption);
i--;
}
}
}
</script>

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.