我正试图让它,当你点击其中一个下拉选择时,它会添加到列表中,这是我们的一个下拉按钮的代码

i am trying to make it so when you click one of the drop down selections, it will add to a list, here is the code of one of our drop down buttons

本文关键字:一个 列表 添加 我们 代码 按钮 当你 选择      更新时间:2023-09-26

我们正在尝试编写一种方法来点击其中一种蔬菜选择,并将其添加到页面一侧的列表中

  <div class="container">
  <h2>
  </h2>
  <p></p> 
  <div class="dropdown">
  <div  id="dropdown" style="position:fixed; left:400px; top:120px;"  class="main">
<button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Vegetable
<span class="caret"></span></button>
<ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
  <li role="presentation"><a role="menutype" tabindex="1" href="#">Carrots</a></li>
  <li role="presentation"><a role="menuitem" tabindex="2" href="#">Tomatos</a></li>
  <li role="presentation"><a role="menuitem" tabindex="3" href="#">Lettuce</a></li>
  <li role="presentation"><a role="menuitem" tabindex="4" href="#">Peas</a></li>
  <li role="presentation"><a role="menuitem" tabindex="5" href="#">Green Beans</a></li>
  <li role="presentation"><a role="menuitem" tabindex="6" href="#">Potatoes</a></li>
  <li role="presentation"><a role="menuitem" tabindex="7" href="#">Corn</a></li>
  <li role="presentation" class="divider"></li>
</ul>
 </div>
</div>

我会创建一个新函数并将其添加到下拉列表中:

onChange="OnSelectedIndexChange()"

并根据您的需要修改此功能。在下拉列表中进行新选择后,它会将li项目附加到您的其他列表中(ul)。也许您需要更改要获取文本而不是值的属性。

//function for changed selection
function OnSelectedIndexChange()
{
document.getElementById('myListOfItems').append("<li>"+document.getElementById('dropdown').value+"</li>");
}