简体   繁体   English

使用 JavaScript 从数组填充下拉菜单

[英]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.

相关问题 使用从 JavaScript 传递的值数组填充下拉列表 - Populating a drop down with a array of values passed from JavaScript 使用JavaScript从服务器端文件填充下拉框 - Populating a drop down box using javascript from server side files Javascript在下拉更改中设置隐藏的表单值-未从Javascript数组填充的选项 - Javascript to set hidden form value on drop down change - options not populating from Javascript array 我正在尝试创建一个下拉菜单,用于存储餐馆菜单中的项目,使用vanilla JavaScript传递一组对象 - I am trying to create a drop down menu which stores items from a restaurant menu, using vanilla JavaScript by passing it an array of objects 从Flask传递数组到Javascript以创建下拉菜单选项 - Passing array from Flask to Javascript to create options for drop down menu 使用javascript填充选择菜单选项 - populating select menu options using javascript 使用带有数据库信息的下拉菜单填充文本框 - populating text boxes using a drop down menu with database information 下拉菜单-从CSS移到使用JavaScript起作用 - Drop down menu - moving from CSS to be functioning using JavaScript JavaScript:使用另一个下拉列表中的onchange数据填充html下拉列表 - Javascript: Populating a html drop-down list based on onchange data from another dropdown using 在Java语言的For循环中从另一个数组填充数组 - Populating Array from Another Array in a For Loop in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM