[英]Populating Drop Menu from Array Using JavaScript
I need to dynamically generate a drop-menu list from an array via JavaScript.我需要通过 JavaScript 从数组中动态生成下拉菜单列表。 I'm getting close, but while I can populate the array elements into the Bootstrap-enabled drop-menu that appears on my index.html page, I cannot select those individual elements that show up as drop menu items.
我快接近了,但是虽然我可以将数组元素填充到我的 index.html 页面上显示的启用 Bootstrap 的下拉菜单中,但我不能 select 显示为下拉菜单项的那些单个元素。 I need to edit my code so I can click on one of those elements and run my second function, that populates the selection to an input field.
我需要编辑我的代码,以便我可以单击其中一个元素并运行我的第二个 function,它将选择填充到输入字段中。
First, here's my script to iterate over the array:首先,这是我迭代数组的脚本:
<script>
let select = document.getElementById("selectJob");
let options = ["Job 1", "Job 2", "Job 3", "Job 4", "Job 5"];
for (let i = 0; i < options.length; i++) {
let opt = options[i];
let el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
</script>
And here's the second script I use:这是我使用的第二个脚本:
<script>
function populateJobVal(val) {
document.getElementById("selection").value = val;
}
</script>
And here's the drop-menu section in my HTML where I'm trying to use this:这是我的 HTML 中的下拉菜单部分,我正在尝试使用它:
<div class="dropdown">
<button class="btn btn-default btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
Select Job Type to Schedule
<span class="caret"></span></button>
<ul class="dropdown-menu" id="selectJob">
<li>
<a href="#" role="button" onclick="populateJobVal(selectJob)" class="btn btn-link">
</a>
</li>
</ul>
</div>
Notice I am adding the "id" to the <ul>
element.请注意,我将“id”添加到
<ul>
元素。 Should it be going elsewhere?是不是应该去别的地方? I did try putting the "id" on the
<li>
element, but that makes all the array elements one clickable field, rather than each one being clickable.我确实尝试将“id”放在
<li>
元素上,但这会使所有数组元素成为一个可点击的字段,而不是每个元素都是可点击的。
Right now, when I click on the drop-menu button, the different jobs do appear.现在,当我单击下拉菜单按钮时,确实会出现不同的作业。 However, they are not individually clickable.
但是,它们不能单独单击。
What do I need to edit in my code to make each element a clickable link?我需要在我的代码中编辑什么以使每个元素成为可点击的链接?
To match what you have templated, you need to do the following:要匹配您的模板,您需要执行以下操作:
let select = document.getElementById("selectJob"); let options = ["Job 1", "Job 2", "Job 3", "Job 4", "Job 5"]; for (let i = 0; i < options.length; i++) { let opt = options[i]; let a = document.createElement("a"); a.textContent = opt; a.setAttribute('href', '#'); a.setAttribute('onclick', 'populateJobVal(selectJob)'); a.setAttribute('class', 'btn btn-link'); let li = document.createElement("li"); li.appendChild(a); select.appendChild(li); } function populateJobVal(val) { document.getElementById("selection").value = val; }
<div class="dropdown"> <button class="btn btn-default btn-primary dropdown-toggle" type="button" data-toggle="dropdown"> Select Job Type to Schedule <span class="caret"></span></button> <ul class="dropdown-menu" id="selectJob"> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.