[英]No options in <select> dropdown when dynamically populated from JS
Image of empty list空列表的图像
Showing populated options within inspector在检查器中显示填充的选项
I dynamically add options through a JS file based on the DB items.我通过基于数据库项的 JS 文件动态添加选项。 They sometimes populate when I CTRL+SHIFT+R hard refresh the page but not every time.
当我 CTRL+SHIFT+R 硬刷新页面时,它们有时会填充,但不是每次都填充。
They never populate when I do a regular refresh (ctrl+r).当我定期刷新 (ctrl+r) 时,它们永远不会填充。
I am genuinely so confused how the options could be populated inside the inspector but not actually visible..我真的很困惑如何在检查器中填充选项但实际上不可见..
The JS is also surrounded with $(document).ready(function ()) so to my knowledge it only runs once the page is ready. JS 也被 $(document).ready(function ()) 包围,所以据我所知它只在页面准备好后运行。
Function where I populate one of the dropdowns我填充下拉菜单之一的功能
function renderEndUseDropdown()
{
var endUseList = new Set(filteredProductList.map((element) => element.endUse));
var endUse = Array.from(endUseList);
var endUseSelection = document.getElementById("processname");
for(var i = 0; i < endUse.length; i++)
{
if(endUse[i] !== '')
{
var option = endUse[i];
var displayElement = document.createElement("option");
displayElement.textContent = option;
displayElement.value = option;
endUseSelection.add(displayElement);
}
}
}
The above code would be adding options to this block of HTML上面的代码将向这个 HTML 块添加选项
<div class="col-xs-12 col-sm-6 col-md-2">
<select class="selectpicker" multiple="multiple" data-live-search="true" title="End Use" id="processname" multiple data-actions-box="true">
</select>
</div>
Since you are using bootstrap-select, this needs to be done.由于您使用的是 bootstrap-select, 因此需要完成此操作。
So your function could be updated like below.所以你的函数可以像下面这样更新。
function renderEndUseDropdown()
{
var endUseList = new Set(filteredProductList.map((element) => element.endUse));
var endUse = Array.from(endUseList);
var endUseSelection = document.getElementById("processname");
for(var i = 0; i < endUse.length; i++)
{
if(endUse[i] !== '')
{
var option = endUse[i];
var displayElement = document.createElement("option");
displayElement.textContent = option;
displayElement.value = option;
endUseSelection.add(displayElement);
}
}
$('#processname').selectpicker('refresh');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.