Initially, I generated a dropdown menu
by:
I have added more details about how I want to make a dropdown menu.
<div class="well sidebar-nav">
<img src="building.gif" style="display: block;margin-left:auto; margin-right:auto;max-width:100%; max-height:100%;" />
<div class="dropdown" style="padding-left: 20px; padding-top:5px;">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" style="display: block;margin-left:auto; margin-right:auto;">
Select A Building
<span class=" caret">
</span>
</button>
<ul id="myul" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"></ul>
</div>
</div>
</div><!--/span2-->
Now, I wrote the following code:
var buildingList=[
{
"BuildingName": "Adler Journalism and Mass Communication Building",
"BuildingNumber": "0456"
},
{
"BuildingName": "Art Building",
"BuildingNumber": "0021"
}
];
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" style="display: block;margin-left:auto; margin-right:auto;">
Select A Building
<span class=" caret">
</span>
</button>
<ul id="myul" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
</ul>
<script type="text/javascript">
var out = "";
var ul = document.getElementById("myul");
for (i = 0; i < buildingList.length; i++) {
out = ' <li role="presentation"><a role="menuitem" tabindex="-1" href=" ' + buildingList[i].BuildingName + ' "> ' + buildingList[i].BuildingName + ' </a></li>';
var li = document.createElement("li");
var t = document.createTextNode(out);
li.appendChild(t);
ul.appendChild(li);
}
</script>
I would like to generate an dropdown
list dynamically based on the buildingList
array using javascript
, can you help me out?
Thanks in advance!
You can try something like this.
$.each(buildingList, function (index, value) {
$(".dropdown-menu").append($("<li>").text(value.BuildingName));
})
I created a fiddle so that you can see.
https://jsfiddle.net/bcub7wsL/1/
I changed the fiddle so that it just uses plain Javascript
You can split the array and append the elements to drop down list. I think this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.