本文實(shí)例講述了JavaScript實(shí)現(xiàn)將數(shù)組數(shù)據(jù)添加到Select下拉框的方法。分享給大家供大家參考。具體如下:
這里演示將數(shù)組中的數(shù)據(jù)添加到Select下拉菜單中的效果,當(dāng)你點(diǎn)擊下拉框的時(shí)候,就動(dòng)態(tài)加載了數(shù)據(jù),更換Select內(nèi)容的時(shí)候,直接替換數(shù)組中的內(nèi)容就可以了。適合前端設(shè)計(jì)者實(shí)現(xiàn)前臺(tái)的部分本地化腳本操作。
運(yùn)行效果截圖如下:

在線演示地址如下:
http://demo.VeVB.COm/js/2015/js-array-add-select-data-codes/
具體代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>將數(shù)組中的數(shù)據(jù)添加到下拉菜單中</title><style type="text/css"><!--.style1 {color: #FFFFFF}--></style><script type="text/javascript">var counts;counts=0;arr = new Array("JavaScript與ASP","JavaScript與JSP","JavaScript與ASP.NET","JavaScript與PHP"); counts=arr.length;function Myselect(){ var i; for (i=0;i < counts; i++) { document.form1.sel.options[i] = new Option(arr[i],i); }}</script></head><body><table width="280" height="160" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td height="34" align="center"><span class="style1" style="font-weight:bold ">將數(shù)組中的數(shù)據(jù)添加到下拉菜單中</span></td> </tr> <tr> <td align="center" valign="top" bgcolor="#9ACCFF"><form name="form1"> <table width="235" height="69" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="21"><select name="sel" id="sel" onFocus="Myselect()"> </select></td> </tr> <tr> <td height="120"> </td> </tr> </table> </form></td> </tr></table></body></html>希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注