我如何获得我网站上的国家列表

时间:2015-04-28 12:31:06

标签: javascript php jquery

我正在尝试找到一种在下拉列表中显示所有国家/地区列表的方法。 php或jquery中是否有任何短代码可以在我的网页上获取国家/地区列表?提前谢谢。

4 个答案:

答案 0 :(得分:7)

我建议使用:

https://github.com/umpirsky/country-list

它为您提供了不同格式的国家/地区列表(文本,JSON,PHP ...)

或者,如果您只想要一个快速HTML列表,请转到:

http://www.textfixer.com/resources/country-dropdowns.php

您可以将这些值复制粘贴到您想要的任何位置,或者您可以调用该页面。

答案 1 :(得分:4)

我使用countries-list npm软件包。

import countries from "countries-list";

获取有关国家的广泛信息:

console.log(countries.countries);

将帮助您

{
   AD: {
      name: "Andorra"
      native: "Andorra"
      phone: "376"
      continent: "EU"
      capital: "Andorra la Vella"
      currency: "EUR"
      languages: ["ca"]
      emoji: "??"
      emojiU: "U+1F1E6 U+1F1E9"
   }
   ...
}

或仅列出国家/地区:

const countryCodes = Object.keys(countries.countries);
const countryNames = countryCodes.map(code => countries.countries[code].name);
console.log(countryNames);

将打印国家名称列表:

"Andorra"
"United Arab Emirates"
"Afghanistan"
"Antigua and Barbuda"
...

在根据国家/地区代码对它们进行排序时,需要按字母顺序对其进行排序:

console.log(countryNames.sort());

答案 2 :(得分:0)

Ruslan Kazakov开始。

如果您正在使用 typescript 和 countries-list npm 包,我发现如果您希望获得下拉列表的国家/地区名称列表,以下内容会让 typescript 编译器满意。

const countryNames = Object.values(countries.countries)
  .map((item) => item.name))
  .sort();

答案 3 :(得分:-1)

在struts2中你可以得到它 - 如,

<s:select name="name" headerKey="-1"
list="countryList" id="country" 
value="%{countryList}" 
></s:select>

在jQuery中,你必须通过ajax调用获取列表并迭代它 e.g,

$.ajax({
           url: 'rcmidAuditTrail.action',
           type: 'GET',
           async : false,
           dataType: 'json',
           error: function(data) {
              console.log('error');
           },
           success: function(list) {

var html = '<select>'
              $.each(list,function(i,value){
                     html+='<option>'+value+'</option>';
              });
$('dropdownId').html(html);
           }
        });