The example in this article describes the JS implementation of traversing a drop-down list that supports multiple selections. Share it with everyone for your reference. The details are as follows:
JS is used here to implement a drop-down list with multiple selections. Click any value in the upper drop-down list with the mouse, and the selected value will be displayed in the lower list. Hold down the Ctrl key on the keyboard and click any value in the upper list again. Value, you can perform multiple selections. The multi-selection function benefits from the help of JavaScript. This kind of application is quite common on web pages. It is necessary to take a look.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-select-option-cha-codes/
The specific code is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>遍历多选择下拉列表</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <style> .wenbenkuang { font-family: "宋体"; font-size: 9pt; color: #333333; border: 1px solid #999999; } </style> </head> <body style="font-size:12px"> <form name="form1" method="post" action=""> <table width="300" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFCC00"> <tr> <td height="22" align="left" bgcolor="#FFFFFF">可以进行多项选择的下拉列表:</td> </tr> <tr> <td align="center" bgcolor="#FFFFFF"><select name="mcusep" size="4" multiple class="wenbenkuang" onchange="updateorder()"> <option value="JavaScript程序设计">JavaScript程序设计</option> <option value="JavaScript经典教程">JavaScript经典教程</option> <option value="JavaScript范例宝典">JavaScript范例宝典</option> <option value="JavaScript与CSS样式">JavaScript与CSS样式</option> <option value="JavaScript与HTML">JavaScript与HTML</option> </select></td> </tr> <tr> <td height="22" align="left" background="images/bg.gif" bgcolor="#FFFFFF">显示所选内容:</td> </tr> <tr> <td align="center" bgcolor="#FFFFFF"><textarea name="textarea" cols="40" rows="6" class="wenbenkuang"></textarea></td> </tr> </table> </form> <script language="javascript"> function updateorder() { var orderstring=""; var n=document.form1.mcusep.length; for (i=0;i<n;++i) { if (document.form1.mcusep.options[i].selected) { orderstring+=document.form1.mcusep.options[i].value+"\n"; } } document.form1.textarea.value=orderstring; } </script> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.