JavaScript实现下拉列表框数据增加、删除、上下排

这里实现在一个支持多选的下拉列表框内进行数据项的添加、删除、向上、向下移动操作,我们在一些人才网站或是编程技术站经常会看到这种功能,比较实用。

运行效果截图如下:

JavaScript实现下拉列表框数据增加、删除、上下排

具体代码如下:

<title>下拉列表数据上下排序</title> <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function move(fbox,tbox) { var i = 0; if(fbox.value != "") { var no = new Option(); no.value = fbox.value; no.text = fbox.value; tbox.options[tbox.options.length] = no; fbox.value = ""; } } function remove(box) { for(var i=0; i<box.options.length; i++) { if(box.options[i].selected && box.options[i] != "") { box.options[i].value = ""; box.options[i].text = ""; } } BumpUp(box); } function BumpUp(abox) { for(var i = 0; i < abox.options.length; i++) { if(abox.options[i].value == "") { for(var j = i; j < abox.options.length - 1; j++) { abox.options[j].value = abox.options[j + 1].value; abox.options[j].text = abox.options[j + 1].text; } var ln = i; break; } } if(ln < abox.options.length) { abox.options.length -= 1; BumpUp(abox); } } function Moveup(dbox) { for(var i = 0; i < dbox.options.length; i++) { if (dbox.options[i].selected && dbox.options[i] != "" && dbox.options[i] != dbox.options[0]) { var tmpval = dbox.options[i].value; var tmpval2 = dbox.options[i].text; dbox.options[i].value = dbox.options[i - 1].value; dbox.options[i].text = dbox.options[i - 1].text dbox.options[i-1].value = tmpval; dbox.options[i-1].text = tmpval2; } } } function Movedown(ebox) { for(var i = 0; i < ebox.options.length; i++) { if (ebox.options[i].selected && ebox.options[i] != "" && ebox.options[i+1] != ebox.options[ebox.options.length]) { var tmpval = ebox.options[i].value; var tmpval2 = ebox.options[i].text; ebox.options[i].value = ebox.options[i+1].value; ebox.options[i].text = ebox.options[i+1].text ebox.options[i+1].value = tmpval; ebox.options[i+1].text = tmpval2; } } } // End --> </script> <form ACTION="" METHOD="POST"> <table> <tr> <td> <input type="button" value="增加"><br> <input type="button" value="删除"><br> <input type="button" value="向上"><br> <input type="button" value="向下"> </td> <td> <select multiple size=7> <option value="one">ASP</option> <option value="two">PHP</option> <option value="three">ASP.NET</option> <option value="four">JAVA</option> <option value="five">DELPHI</option> </select> </td> </tr> <tr> <td> <input type="text" value=""> </td></tr> </table> </form>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wggpyg.html