javascript - 使用 JSON 对象元素填充选择选项
问题描述
我想使用 JSON Object 中包含的选项填充选择标记:
您好需要填充此选择:
<select class="selectpicker" name="iroleinspecteurcommercial" id="iroleinspecteurcommercial" data-live-search="true" data-actions-box="true" multiple></select></br>
来自的信息arr1.libellerole
我试过这段代码但没有结果:
$.each(arr1.libellerole, function(k, v){
$("#iselectroledirregional").append('<option>'+v+'</option>');
});
谢谢
解决方案
Based on the image you've provided, there is no arr1.libellerole
. Instead, you have to iterate over every element in the array and access its libellerole
property.
Also, the id of the <select>
you've given and the one in your JavaScript code don't match.
Try this:
$.each(arr1, function (k, v) {
$("#iroleinspecteurcommercial").append('<option>' + v.libellerole + '</option>');
});
Example:
var arr1 = [
{libellerole: 1},
{libellerole: 2},
{libellerole: 3},
{libellerole: 4},
{libellerole: 5}
];
$.each(arr1, function(k, v) {
$("#iroleinspecteurcommercial").append('<option>' + v.libellerole + '</option>');
});
<script src = "//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id = "iroleinspecteurcommercial" multiple></select>
推荐阅读
- azure-cosmosdb - 能否共享 Azure Cosmos DB 更改源的租约集合?
- magento2 - 致命错误:未捕获的错误:找不到类'Magento\Framework\Component\ComponentRegistrar'
- php - Memcached 不保存生成的数据
- node.js - 在对话节点alexa-sdk期间更换卡片
- django - 显示来自多对多字段的所有数据
- elasticsearch - 计算每个桶的嵌套聚合百分比
- firebase - 无法将 NodeMCU 与 Firebase 连接,因为控制台显示设置/编号失败:每次
- c# - C#中的伪朋友类
- java - Groovy 如何使用正则表达式在地图中查找值
- python-3.x - Python - 使用 ON DUPLICATE KEY UPDATE 时出现“未知编码:utf8mb4”