效果圖如下: 代碼如下: <html> <head> <title>Javascript版選擇下拉菜單互移且排序</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <
效果圖如下:
代碼如下:
<html> <head> <title>Javascript版選擇下拉菜單互移且排序</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <p>選定一項或多項然後點擊添加或移除(按住shift或ctrl可以多選),或在選擇項上雙擊進行添加和移除。</p> <form method="post" name="myform"> <table border="0" width="300"> <tr> <td width="40%"> <select style="width:100px; height:200px" multiple name="list1" size="12" ondblclick="moveOption(document.myform.list1, document.myform.list2)"> <option value="北京">北京</option> <option value="上海">上海</option> <option value="山東">山東</option> <option value="安徽">安徽</option> <option value="重慶">重慶</option> <option value="福建">福建</option> <option value="甘肅">甘肅</option> <option value="廣東">廣東</option> <option value="廣西">廣西</option> <option value="貴州">貴州</option> <option value="海南">海南</option> <option value="河北">河北</option> <option value="黑龍江">黑龍江</option> <option value="河南">河南</option> <option value="湖北">湖北</option> <option value="湖南">湖南</option> <option value="內蒙古">內蒙古</option> <option value="江蘇">江蘇</option> <option value="江西">江西</option> <option value="吉林">吉林</option> <option value="遼寧">遼寧</option> <option value="寧夏">寧夏</option> <option value="青海">青海</option> <option value="山西">山西</option> <option value="陝西">陝西</option> <option value="四川">四川</option> <option value="天津">天津</option> <option value="西藏">西藏</option> <option value="新疆">新疆</option> <option value="雲南">雲南</option> <option value="浙江">浙江</option> <option value="香港">香港</option> <option value="澳門">澳門</option> <option value="臺灣">臺灣</option> <option value="其他">其他</option> </select> </td> <td width="20%" align="center"> <input type="button" value=">>" onclick="moveOption(document.myform.list1, document.myform.list2)"><br /> <br /> <input type="button" value="<<" onclick="moveOption(document.myform.list2, document.myform.list1)"> </td> <td width="40%"> <select style="width:100px; height:200px" multiple name="list2" size="12" ondblclick="moveOption(document.myform.list2, document.myform.list1)"> </select> </td> <td> <button onclick="changepos(list2,-1)" type="button"> ∧</button> <br /> <button onclick="changepos(list2,1)" type="button"> ∨</button> </td> </tr> </table> 值:<input type="text" name="city" size="40"> </form> <script language="JavaScript"> function moveOption(e1, e2) { try { for (var i = 0; i < e1.options.length; i++) { var e = e1.options[i]; if (e1.options[i].selected&& OptionExists(e2, e.value)) { e2.options.add(new Option(e.text, e.value)); e1.remove(i); i = i - 1 } } document.myform.city.value = getvalue(document.myform.list2); } catch (e) { } } function getvalue(geto) { var allvalue = ""; for (var i = 0; i < geto.options.length; i++) { allvalue += geto.options[i].value + ","; } return allvalue; } function changepos(obj, index) { if (index == -1) { if (obj.selectedIndex > 0) { obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex - 1)) } } else if (index == 1) { if (obj.selectedIndex < obj.options.length - 1) { obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex + 1)) } } } //查詢是否已經存在 function OptionExists(list, optValue) { var find = true; for (i = 0; i < list.options.length; i++) { if (list.options[i].value == optValue) { find = false; break; } } return find; } </script> </body> </html>