Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript向下拉列表添加选项_Javascript_List_Drop Down Menu - Fatal编程技术网

使用javascript向下拉列表添加选项

使用javascript向下拉列表添加选项,javascript,list,drop-down-menu,Javascript,List,Drop Down Menu,我需要在下拉列表中添加选项。我在下拉列表中有一个包含所需选项的数组,但我不确定如何将该数组附加到列表中 var productList = document.getElementById("selectFood"); var foodOption = document.createElement("option"); foodOption.textContent = [{ option1: "Meat Lovers" }, { option2: "Buffalo Wings"

我需要在下拉列表中添加选项。我在下拉列表中有一个包含所需选项的数组,但我不确定如何将该数组附加到列表中

var productList = document.getElementById("selectFood");
var foodOption = document.createElement("option");

foodOption.textContent = [{ 
    option1: "Meat Lovers"
}, {
    option2: "Buffalo Wings"
}, {
    option3: "margherita"
}, {
    option4: "classic"
}];

for (i = 0; i < productList.length; i++) { 
    productList.appendChild(foodOption);
}
var productList=document.getElementById(“selectFood”);
var foodOption=document.createElement(“选项”);
foodOption.textContent=[{
选项1:“肉食爱好者”
}, {
选项2:“水牛翼”
}, {
选项3:“玛格丽塔”
}, {
选项4:“经典”
}];
对于(i=0;i
您可以使用jquery

$.each(foodOption.textContent, function (i, value) {
    $('#selectFood').append($('<option>').text(value).attr('value',value));
});
$。每个(foodOption.textContent,函数(i,值){
$('#selectFood').append($('').text(value.attr('value',value));
});

请尝试以下解决方案的代码

var productList = document.getElementById("selectFood");
var textContent = [{ 
    option1: "Meat Lovers"
}, {
    option2: "Buffalo Wings"
}, {
    option3: "margherita"
}, {
    option4: "classic"
}];

for(var iIntex in textContent) {
    var foodOption = document.createElement("option");
    var option=textContent[iIndex]["option"+(iIndex+1)];
    foodOption.setAttribute("value",option);
    foodOption.innerHTML=option;
    productList.appendChild(foodOption);
}

您需要创建一个选项元素

for (i = 0; i < foodOption.textContent.length; i++) {
   var newElement = document.createElement('option');
   newElement.innerHTML = foodOption.textContent[i];
   productList.appendChild(newElement);
}
for(i=0;i
您必须创建字符串数组,而不是对象数组

试试这个

 foodOption.textContent = ["Meat Lovers" , "Buffalo Wings" , "margherita", "classic" ]


$.each(foodOption.textContent, function (index, value) {

    $('#selectFood').append($('<option/>', { value: value, text: value }));

});
foodOption.textContent=[“肉食爱好者”、“水牛翅”、“玛格丽塔”、“经典”]
$.each(foodOption.textContent,函数(索引,值){
$('#selectFood').append($('',{value:value,text:value}));
});

这个
textContent
数组非常奇怪。它是一个对象数组,其中每个对象只有一个属性,并且所有属性都有不同的名称。如果你能改变它,我建议你改变它。他要求用java脚本来解决。但是这个jQuery看起来很不错,我做的option元素是
foodOption
@Matt你附加所有对象你创建的foodOption是javascript中的一个变量,你需要先创建一个html option元素才能将它转换成html。然后,您需要附加该元素。在代码中,每次运行for时都会尝试附加一个数组。你明白其中的区别吗?