Javascript - 在国家/地区下拉列表更改时填充国家/地区代码下拉列表
Javascript - Populate country code drop down onChange of country dropdown
是否有任何 Javascript 插件可以在更改国家下拉列表时填充国家代码?
插件,我不知道...我知道答案不应该只包含外部链接,但我想这可能是个例外,我会包含一些链接以防有一天 1 中断.. .
由于现在国家名称和代码不会经常更改,因此使用此文本摘录可能是安全的:
http://www.textfixer.com/resources/dropdowns/country-list-iso-codes.txt
然后使用 split(':') 函数,轻松填充 select 列表的文本和值
像这样的选项元素:
function populateCountriesDropDown() {
var file = "countries.txt";
var selectList = document.getElementById('selectID');
var rawlist;
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
rawlist = rawFile.responseText.split('\n');
}
}
}
rawFile.send(null);
for (var i = 0; i < rawlist.length; i++) {
var country = rawlist[i].split(':');
selectList.options[selectList.options.length] = new Option(country[1], country[0]);
}
}
或与您可能正在寻找的内容相关的其他链接:
http://www.freeformatter.com/iso-country-list-html-select.html
是否有任何 Javascript 插件可以在更改国家下拉列表时填充国家代码?
插件,我不知道...我知道答案不应该只包含外部链接,但我想这可能是个例外,我会包含一些链接以防有一天 1 中断.. . 由于现在国家名称和代码不会经常更改,因此使用此文本摘录可能是安全的:
http://www.textfixer.com/resources/dropdowns/country-list-iso-codes.txt
然后使用 split(':') 函数,轻松填充 select 列表的文本和值 像这样的选项元素:
function populateCountriesDropDown() {
var file = "countries.txt";
var selectList = document.getElementById('selectID');
var rawlist;
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
rawlist = rawFile.responseText.split('\n');
}
}
}
rawFile.send(null);
for (var i = 0; i < rawlist.length; i++) {
var country = rawlist[i].split(':');
selectList.options[selectList.options.length] = new Option(country[1], country[0]);
}
}
或与您可能正在寻找的内容相关的其他链接:
http://www.freeformatter.com/iso-country-list-html-select.html