【发布时间】:2019-05-04 13:42:10
【问题描述】:
我有 JSON 格式的图片网址,如下所示:
var obj = [
{
country: "SG",
images: "../images/flag/singapore.jpg"
},
{
country: "TH",
images: "../images/flag/thailand.jpg"
}
]
function render() {
return `
<select name="sourcecountrycode" id="sourcecountrycode">
${obj.map((cn) => `
<option value=${cn.country}>${cn.country}</option>`)}
// here options SG, TH are generated in select tag dynamically
</select>
`
}
$('#block').html(render());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="block">
</div>
如何使用 JavaScript 将图片添加到 select 中的相应选项?
【问题讨论】:
-
如果我是我,我会选择组件库。重新发明下拉菜单没有意义...semantic-ui.com/modules/dropdown.html“搜索选择”演示看起来很适合您。
标签: javascript arrays html object