代码直接复制可运行:
<%-- Created by IntelliJ IDEA. User: Freddy Date: 2016/9/13 Time: 10:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <SCRIPT LANGUAGE="JavaScript"> <!-- //上移 function moveUp(obj) { for(var i=1; i < obj.length; i++) {//最上面的一个不需要移动,所以直接从i=1开始 if(obj.options[i].selected) { if(!obj.options.item(i-1).selected) { var selText = obj.options[i].text; var selValue = obj.options[i].value; obj.options[i].text = obj.options[i-1].text; obj.options[i].value = obj.options[i-1].value; obj.options[i].selected = false; obj.options[i-1].text = selText; obj.options[i-1].value = selValue; obj.options[i-1].selected=true; } } } } //下移 function moveDown(obj) { for(var i = obj.length -2 ; i >= 0; i--) {//向下移动,最后一个不需要处理,所以直接从倒数第二个开始 if(obj.options[i].selected) { if(!obj.options[i+1].selected) { var selText = obj.options[i].text; var selValue = obj.options[i].value; obj.options[i].text = obj.options[i+1].text; obj.options[i].value = obj.options[i+1].value; obj.options[i].selected = false; obj.options[i+1].text = selText; obj.options[i+1].value = selValue; obj.options[i+1].selected=true; } } } } //移动 function moveOption(obj1, obj2) { for(var i = obj1.options.length - 1 ; i >= 0 ; i--) { if(obj1.options[i].selected) { var opt = new Option(obj1.options[i].text,obj1.options[i].value); opt.selected = true; obj2.options.add(opt); obj1.remove(i); } } } //置顶 function moveTop(obj) { var opts = []; for(var i =obj.options.length -1 ; i >= 0; i--) { if(obj.options[i].selected) { opts.push(obj.options[i]); obj.remove(i); } } var index = 0 ; for(var t = opts.length-1 ; t>=0 ; t--) { var opt = new Option(opts[t].text,opts[t].value); opt.selected = true; obj.options.add(opt, index++); } } //置底 function moveBottom(obj) { var opts = []; for(var i =obj.options.length -1 ; i >= 0; i--) { if(obj.options[i].selected) { opts.push(obj.options[i]); obj.remove(i); } } for(var t = opts.length-1 ; t>=0 ; t--) { var opt = new Option(opts[t].text,opts[t].value); opt.selected = true; obj.options.add(opt); } } //置顶 function saveButton(obj) { alert("暂时没有做:TODO"); } //--> </SCRIPT> <body> <span id='feedback'></span> <form method="post" name="myform"> <table border="0" width="400"> <tr> <td><CENTER>可选择车辆信息</CENTER></td> <td> </td> <td><CENTER>要添加到的上海金桥4S围栏</CENTER></td> </tr> <tr> <td width="40%"> <select multiple name="left" id="left" size="6" style='width:200;' οndblclick="moveOption(document.getElementById('left'), document.getElementById('right'))"> <option value="20">车辆一</option> <option value="30">车辆二</option> <option value="40">车辆三</option> <option value="50">车辆四</option> <option value="60">车辆五</option> <option value="70">车辆六</option> <option value="80">车辆七</option> <option value="90">车辆八</option> </select> </td> <td width="20%" align="center"> <input type="button" value="添加(可全选)" οnclick="moveOption(document.getElementById('left'),document.getElementById('right'))"> <input type="button" value="移除(可全选)" οnclick="moveOption(document.getElementById('right'), document.getElementById('left'))"> </td> <td width="40%"> <select multiple name="right" id="right" size="6" style='width:200;' οndblclick="moveOption(document.getElementById('right'), document.getElementById('left'))"> </select> </td> </tr> <tr> <td colspan="3"> <CENTER> <INPUT TYPE="button" value="保存" οnclick="saveButton(document.getElementById('right'));"> <INPUT TYPE="button" value="取消" οnclick="moveOption(document.getElementById('right'), document.getElementById('left'))"> </CENTER></td> </tr> <%-- <tr> <td colspan="3"> <CENTER> <INPUT TYPE="button" value="置顶" οnclick="moveTop(document.getElementById('right'));"> <INPUT TYPE="button" value="上移" οnclick="moveUp(document.getElementById('right'));"> <INPUT TYPE="button" value="下移" οnclick="moveDown(document.getElementById('right'));"> <INPUT TYPE="button" value="置底" οnclick="moveBottom(document.getElementById('right'));"> </CENTER></td> </tr>--%> </table> </body> </html>