2015-12-07 363 views
-1

的的onChange是否有任何JavaScript插件以填充國家下拉的變化國家代碼?使用Javascript - 填寫國家代碼下拉國家下拉

+0

能否請您告訴我們您的'html'代碼(至少)。我猜你有2個下拉菜單。當第一個變化你想填充另一個。對? ('在國家的變化上填充國家代碼嗎? - 國家? –

+0

@Shyam爲了更加清晰,我已經用完整的代碼示例更新了我的答案 – EaziLuizi

回答

0

插件,不,我知道......我知道答案,不應該只包含外部鏈接,但我想這可能是例外,我將包括萬一有一天休息1幾個環節... 由於國家名稱和代碼不改變過於頻繁現在可能是安全與這段文字摘錄:

http://www.textfixer.com/resources/dropdowns/country-list-iso-codes.txt

然後使用split(':')功能,便於填充文本選擇列表 選項元素這樣的&值:

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

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