JavaScript如何?組合列表框中元素移動效果_javascript技巧

來源:互聯網
上載者:User

 首先給大家說下組合框和列表框的區別:

組合框包括列表框和文字框的功能

文字框:只能輸入資料

列表框:只能選擇資料

組合框:既能輸入資料,又能選擇``

應用背景:在頁面中有兩個列表框,需要把其中一個列表框的元素移動到另一個列表框 。

   實現的基本思想:

  (1)編寫init方法對兩個列表框進行初始化;

  (2)為body添加onload事件調用init方法;

  (3)編寫move(s1,s2)把s1中選中的選項移到s2;

  (4)編寫moveAll(s1,s2)把s1中所有的選項都移到s2.

  (5)為按鈕添加onclick事件。

javascript代碼如下:

<script type="text/javascript" language="javascript">//對下拉框資訊進行初始化function init() {for (i = ; i < ; i++) {var y = document.createElement("option");//增加一個元素optiony.text = '選項' + i;var x=document.getElementById("s");//根據ID找到列表框x.add(y, null); //}}//把選中的選項移到另一邊function move(s, s) {var index = s.selectedIndex;if (index == -) {alert("沒有選中值");return;}s.length++;s.options[s.length - ].value = s.options[index].value;s.options[s.length - ].text = s.options[index].text;//s中當前選中的值賦給s的最後一個元素s.remove(index);//從s中移除當前元素}//把一邊的完全移到另一邊function moveAll(s, s) {if (s.length == ) {alert("沒有可用選擇");return;}s.length = s.length + s.length;for (var i = ; i < s.length; i++) {s.options[s.length - s.length + i].value = s.options[i].value;s.options[s.length - s.length + i].text = s.options[i].text;}s.length = ;}</script> 

<body>代碼:

<body onload="init()"><table><tr><td><select id="s" size= style="width:"></select></td><td><input type="button" name="moveToRight" value=">"onClick="move(s,s)"> <br><br> <input type="button" name="moveAllToRight" value=">>"onClick="moveAll(s,s)"> <br> <input type="button"name="moveToLeft" value="<" onClick="move(s,s)"> <br><br> <input type="button" name="moveAllToLeft" value="<<"onClick="moveAll(s,s)"></td><td><select id="s" name="s" size= style="width:"></select></td></tr></table></body>

以上內容給大家介紹了JavaScript如何?組合列表框中元素移動效果的相關知識,希望對大家有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.