使用JS动态创建国家下拉列表[重复]

Posted

技术标签:

【中文标题】使用JS动态创建国家下拉列表[重复]【英文标题】:Dynamically creating dropdown of countries using JS [duplicate] 【发布时间】:2021-11-22 13:15:40 【问题描述】:

使用 javascript 从多个国家/地区动态创建国家/地区下拉列表。使用国家简码数组,我们可以仅使用 javascript 创建一个选择下拉列表吗?哪个循环更适合这项任务? For 循环或 for in 循环或任何其他?

【问题讨论】:

复制:JavaScript - populate drop down list with array 任何类型的循环都可以。 【参考方案1】:

let countriesArray = ["GB","US","AF","AL","DZ","AS","AD","AO","AI","AQ","AG","AR","AM","AW","AU","AT","AZ","BS","BH","BD","BB","BY","BE","BZ","BJ","BM","BT","BO","BA","BW","BV","BR","IO","BN","BG","BF","BI","KH","CM","CA","CV","KY","CF","TD","CL","CN","CX","CC","CO","KM","CG","CD","CK","CR","CI","HR","CU","CY","CZ","DK","DJ","DM","DO","EC","EG","SV","GQ","ER","EE","ET","FK","FO","FJ","FI","FR","GF","GG","PF","TF","GA","GM","GE","DE","GH","GI","GR","GL","GD","GP","GU","GT","GN","GW","GY","HT","HM","VA","HN","HK","HU","IS","IN","ID","IR","IQ","IE","IL","IM","IT","JE","JM","JP","JO","KZ","KE","KI","KP","KR","KW","KG","LA","LV","LB","LS","LR","LY","LI","LT","LU","MO","MK","MG","MW","MY","MV","ML","MT","MH","MQ","MR","MU","YT","MX","FM","MD","MC","MN","ME","MS","MA","MZ","MM","NA","NR","NP","NL","AN","NC","NZ","NI","NE","NG","NU","NF","MP","NO","OM","PK","PW","PS","PA","PG","PY","PE","PH","PN","PL","PT","PR","QA","RE","RO","RU","RW","SH","KN","LC","PM","VC","WS","SM","ST","SA","SN","RS","SC","SL","SG","SK","SI","SB","SO","ZA","GS","ES","LK","SD","SR","SJ","SZ","SE","CH","SY","TW","TJ","TZ","TH","TL","TG","TK","TO","TT","TN","TR","TM","TC","TV","UG","UA","AE","UM","UY","UZ","VU","VE","VN","VG","VI","WF","EH","YE","ZM"];
let countriesOptions = "";
for(y in countriesArray)

  countriesOptions += "<option value="+countriesArray[y]+">"+countriesArray[y]+"</option>";

let dropdown = "<select><option value=''></option>"+countriesOptions+"</select>";
document.write("Select your country : "+dropdown);

【讨论】:

以上是关于使用JS动态创建国家下拉列表[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Codeigniter 上的动态相关下拉列表国家和州

动态创建多个下拉列表

通过jQuery根据位置动态重新排列国家下拉列表?

使用jquery mobile创建动态下拉列表

使用 XSD 1.1 的动态枚举限制

如何在选择时使用 formArray 索引加载动态下拉数据