使用JavaScript动态生成选择选项下拉菜单

使用JavaScript动态生成选择选项下拉菜单,javascript,html,drop-down-menu,html-select,Javascript,Html,Drop Down Menu,Html Select,这是我要创建的选择菜单。但正如您所看到的,存在静态数据。由于我实际上从后端servlet传递了大量JSON数据,因此将有数百个选项项。我想为我的下拉框动态生成选项菜单 <select id="brand-select" name="brand"> <option value="audi">Audi</option> <option value="bmw">BMW</option> <option value

这是我要创建的选择菜单。但正如您所看到的,存在静态数据。由于我实际上从后端servlet传递了大量JSON数据,因此将有数百个选项项。我想为我的下拉框动态生成选项菜单

<select id="brand-select" name="brand">
    <option value="audi">Audi</option>
    <option value="bmw">BMW</option>
    <option value="lexus">Lexus</option>
</select>
这是我的试用期,不起作用:

HTML:

JavaScript:

//Asssume I have jsonDataForBrands ready, and it contains 100s items
function createOptions() {
    for (var fieldIndex in jsonDataForBrands) {
        html += '<option value=' + fieldIndex  + '>' + jsonDataForBrands[field].title + '</option>';
}
function creatBrandOptions(){
    for (var field in jsonDataForBrands) {
         $('<option value="'+ jsonDataForBrands[field].name +'">' + jsonDataForBrands[field].title + '</option>').appendTo('#brand-select');
    }
}

我自己想出来的

HTML:

JavaScript:

//Asssume I have jsonDataForBrands ready, and it contains 100s items
function createOptions() {
    for (var fieldIndex in jsonDataForBrands) {
        html += '<option value=' + fieldIndex  + '>' + jsonDataForBrands[field].title + '</option>';
}
function creatBrandOptions(){
    for (var field in jsonDataForBrands) {
         $('<option value="'+ jsonDataForBrands[field].name +'">' + jsonDataForBrands[field].title + '</option>').appendTo('#brand-select');
    }
}

我做了一个Ajax调用,从servlet中检索JSON数据,然后在同一个函数中创建BrandOptions,顺便说一句。

本例使用jQuery。看看是否适合你:

function createOptions( jsonDataForBrands ) {
   $('#brand-select option').remove(); // first remove all options
   for (var i=0; i<jsonDataForBrands.length; i++)

   for (var fieldIndex in jsonDataForBrands) { // then populatem them
      $('#brand-select').append($("<option></option>").attr("value", fieldIndex  ).text(jsonDataForBrands[field].title) );
}

你赢了我。下面是使用jquery向select添加选项的完整简化示例:

<html>
<head>
<script src="jquery.js"></script>

</head>
<body>
<button id="populateMenu" >Populate menu</button>

<select id="mySelect">


</select>

<script>
    var optionValues= [[1,"first"],[2,"second"]];

    jQuery("#populateMenu").click( function () {
        for (var i=0;i<optionValues.length;i++){
            jQuery('#mySelect').append(jQuery("<option></option>").val(optionValues[i][0]).text(optionValues[i][1]))
        }
    });
</script>
</body>
</html>

json的具体结构是什么?请暂时忽略json内容,因为我已经提取了所有数据,jsonDataForBrands[1]。title=bmw jsonDataForBrands[2]。title=VW jsonDataForBrands[3]。title=Benz。。等我只需要将这个品牌数据填充到下拉框的“optionmenu”中,干杯答案是否符合您的需要?不完全是…我认为这不是同一类型的问题。。我的事情其实很简单…一句话。。。我需要一个函数来为我的选择下拉框创建100个选项…数据都已经存在于jsonDataForBrands[1]。title,jsonDataForBrands[2]。title,jsonDataForBrands[3]。title…等等…那么这些JSON是否存储在数组中呢。。?您需要提供有关您拥有的数据结构的更多信息。感谢您这样做,以避免在用户更改选项时使用“上一个”选项在菜单上填充。在您的语句var$select=$“mySelect”之前添加下面的选项;$select.find'option'。删除;