【问题标题】:how do i get a list of countries in my website如何在我的网站中获取国家/地区列表
【发布时间】:2015-04-28 12:31:05
【问题描述】:

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

【问题讨论】:

  • 请显示您当前的尝试,如果有的话。

标签: javascript php jquery


【解决方案1】:

我建议使用:

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

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

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

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

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

【讨论】:

  • 非常感谢卡马尔。
  • 好吧,我需要在数据库中插入所有国家,因为我必须在 php 中使用它吗?
【解决方案2】:

我使用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());

【讨论】:

    【解决方案3】:

    最新答案。

    支持 Intl 的浏览器现在可以使用您选择的语言为您提供国家/地区。

    function getCountries(lang = 'en') {
        const A = 65
        const Z = 90
        const countryName = new Intl.DisplayNames([lang], { type: 'region' });
        const countries = {}
        for(let i=A; i<=Z; ++i) {
            for(let j=A; j<=Z; ++j) {
                let code = String.fromCharCode(i) + String.fromCharCode(j)
                let name = countryName.of(code)
                if (code !== name) {
                    countries[code] = name
                }
            }
        }
        return countries
    }
    

    调用getCountries() 将返回一个国家代码对象列表及其名称,例如。

    {AC: 'Ascension Island', AD: 'Andorra', AE: 'United Arab Emirates', AF: 'Afghanistan', AG: 'Antigua & Barbuda', …}
    

    【讨论】:

    • 这太棒了。对于查看此处的其他人的上下文,该解决方案循环遍历两个字符 (AA, AB, AC, …) 的所有可能组合,并检查 Intl 是否知道它是有效的国家/地区代码。
    • 如果您还想要映射到国家代码的名称列表,您可以使用Object.fromEntries(Object.entries(COUNTRY_CODES_TO_NAMES).map(entry =&gt; [entry[1], entry[0]])) 其中COUNTRY_CODES_TO_NAMES 是@Gerardlamo 函数的输出。
    【解决方案4】:

    Ruslan Kazakov开始。

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

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

    【讨论】:

      【解决方案5】:

      在struts2中你可以得到它—— 例如,

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

      在 jQuery 中,您必须通过 ajax 调用获取列表并对其进行迭代 例如,

      $.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);
                 }
              });
      

      【讨论】:

        猜你喜欢
        • 2015-03-08
        • 1970-01-01
        • 2010-10-04
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        相关资源
        最近更新 更多