javascript - Html select dropdownlist如何同时显示name和id
问题描述
我一直在寻找同样的问题但没有找到,我想要实现的是,我想在选项上同时显示 'nama' 和 'id',所以当单击下拉列表时应该显示 'wick dhdfyj97 ' 和 'abraham 15hndfj',这可能吗(在 javascript 中)?现在我已经设法只显示'nama'。这就是我的 json 的样子:
[{"nama ": "wick","id": "dhdfyj97",},{"nama ": "abraham","id": "15hndfj",}]
这是代码:
.then(res =>{
res.json().then(num => {
for (var o = 0; o < num.length; o++) {
var i = document.getElementById("Select");
var d = document.createElement("option");
d.text = num[o].nama;
i.add(d);
}
}
)
})
解决方案
将 nama 和 id 连接在一起:
.then(res =>{
res.json().then(num => {
for (var o = 0; o < num.length; o++) {
var i = document.getElementById("Select");
var d = document.createElement("option");
d.text = num[o].nama + ' ' + num[o].id;
i.add(d);
}
}
)
})
推荐阅读
- javascript - 图标点击显示日期
- alfresco - 如何将自定义页面添加到复制 Alfresco Share 外观/感觉的 Alfresco Share?
- javascript - ResizeObserver 调用期间未捕获的 TypeError:chartObj.width 不是函数
- c++ - 自定义合并和 std::inplace_merge 之间的区别
- java - 我需要帮助创建这个 Java 地图吗?
- chisel - 使用 peekpoketester 戳单个位
- html - 如何使页脚 div 始终位于页面内容的底部
- ssl - Tomcat 9 配置让我们加密证书
- r - 润滑后 R 类变化
- excel - 没有聚合的 Excel 数据透视表 (mac)