gpt4 book ai didi

javascript - 如何使用 jquery/javascript 在 html 选择选项中填充 javascript 对象值的数据

转载 作者:行者123 更新时间:2023-11-30 10:14:38 25 4
gpt4 key购买 nike

javascript 对象 命名国家。现在我想在 HTML 中填充此数据选择

var countries = {
India:"delhi",
US:"Norway",
UK:"London",
id: 123752,
allcountries : function(){return this.India +" "+ this.US + " " + this.UK + " "+this.id}
};

HTML 代码

<select id="con">
<option></option>
</select>

我试过了

document.getElementById("con").innerHTML = countries.allcountries();

$('#con option').append(countries.India);

但它不起作用任何人都可以帮助我在 html 选择中使用 jquery/javascript 填充数据。

最佳答案

尝试,

var countries = {
India: "delhi",
US: "Norway",
UK: "London"
};

$('#con').html($.map(countries, function (val, key) {
return '<option value="' + key + '">' + val + '</option>';
}).join(''));

DEMO

关于javascript - 如何使用 jquery/javascript 在 html 选择选项中填充 javascript 对象值的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24546640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com