简体   繁体   English

如何使用html dom为国家/地区列表创建对象数组?

[英]How to create an object array for a country list using html dom?

Got stuck with this one for awhile now. 现在卡住了一段时间。 Any help would be appreciated. 任何帮助,将不胜感激。

I am making a select element with country options for my website. 我正在为我的网站选择一个带有国家/地区选项的元素。 I have a javascript file with 2-object array country list (I can't specify the options in html file, only use this js file): 我有一个带有2对象数组国家列表的javascript文件(我无法在html文件中指定选项,只能使用此js文件):

var country_list = [
{"country_code": "CA", "country_name": "Canada"},
{"country_code": "UK", "country_name": "United Kingdom"},
{"country_code": "AU", "country_name": "Australia"},
{"country_code": "NZ", "country_name": "New Zealand"} ];

And then here is my html file: 然后这是我的html文件:

<form name="form1">    
Country <select value="country_code" onclick="select_country()"></select>
</form>

Country names have to be showed on the dropdown list, while the values for the options will be 2-letter country codes. 国家名称必须显示在下拉列表中,而选项的值将是2个字母的国家/地区代码。 Plus, Australia has to be selected by default. 另外,默认情况下必须选择澳大利亚。

This is what I have been up to so far: 到目前为止,这是我一直在做的事情:

function select_country(){
var select = document.form1.createElement("SELECT");
select.setAttribute("id","mySelect");
document.form1.body.appendChild(select);
var option = document.form1.createElement("option");
option.setAttribute("value", "Canada");
var text = document.createTextNode("CAN");
option.appendChild(text);
document.form1.getElementById("mySelect").appendChild(option);
}

You can do this pretty easily with vanilla js: 您可以使用vanilla js轻松完成此操作:

var selectElem = document.createElement("select");
for (var i = 0; i < country_list.length; i++) {
    var option = document.createElement("option");
    option.text = country_list[i].country_name;
    option.value = country_list[i].country_code;

    if (option.text == "Australia") option.selected = true; //default option
    selectElem.appendChild(option);
}

document.form1.body.appendChild(selectElem);
 for (i=0; i <=country_list.length; i++){
    var select = document.form1.createElement("SELECT");
    select.setAttribute("id","mySelect"+i);
    document.form1.body.appendChild(select);
    var option = document.form1.createElement("option");
    option.setAttribute("value", country_list[i].country_name);
    var text = document.createTextNode(country_list[i].country_code);
    option.appendChild(text);
    document.form1.getElementById("mySelect"+i).appendChild(option);
 }

If you would like to use jQuery to do it: 如果您想使用jQuery来做到这一点:

var country_list = [{
    "country_code": "CA",
    "country_name": "Canada"
}, {
    "country_code": "UK",
    "country_name": "United Kingdom"
}, {
    "country_code": "AU",
    "country_name": "Australia"
}, {
    "country_code": "NZ",
    "country_name": "New Zealand"
}]
.forEach(function(e){
    $("<option value='"+ e.country_code+"'>"+e.country_name+"</option>")
    .appendTo("select");
});

fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript中的数组创建国家/地区列表? 我在页面中总共有10个或更多选择国家/地区框 - How to create country list using array in javascript? i have total 10 or more select country box in page 如何使用DOM而不是HTML串联创建带有格式化项的列表? - How to create a list with formatted items using DOM instead on HTML concatenation? 如何使用javascript创建html dom的叶节点数组 - How to create an array of leaf nodes of an html dom using javascript 如何使用国家/地区的所有城市创建输入列表html5? 有可能吗 - How create an input list html5 with all cities of country ? Is possible? 如何循环一个多维对象数组然后创建一个 HTML 嵌套列表? - How to loop an Multidimentional Object-Array then create a HTML Nested List? 如何创建一个html对象,并使用JQuery从数组中添加同级对象? - How to create an html object and append sibling to that from an array using JQuery? 如何从代码创建HTML文档DOM对象? - How to create a HTML document DOM object from code? 如何从通过XMLHttpRequest接收的html页面创建DOM对象? - How to create DOM object from html page received over XMLHttpRequest? 如何使用innerHTML在html中显示HTML DOM对象 - How to show HTML DOM object in html using innerHTML 如何通过在DOM中查找变量来创建变量数组或对象? - How to create an array or object of variables by looking them in the DOM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM