所以当谈到javascript时,我是一个真正的菜鸟。 我在网站上尝试了所有解决方案,我从谷歌检查了一些其他的东西,但我找不到任何对我有用的东西。
我必须创建一个json数组并将其导入到选择选项列表中。目标是从列表框中选择音乐标题。
这是我当前的html代码(如果您需要更多代码告诉我,现在我将发布我认为必要的内容):
<td><input id="anzahl" type="number" min="1" max="100"></td>
<td><select id="mySelect" name="Titel">
<option id="01"></option>
<option id="02"></option>
<option id="03"></option>
<option id="04"></option>
<option id="05"></option>
<option id="06"></option>
<option id="07"></option>
<option id="08"></option>
<option id="09"></option>
<option id="10"></option>
</select></td>
...
编辑:这是更新的代码,但它仍然无效。
function initSelBox_Product() {
var titelliste= [
{"Produktid":"01","Titel":"Dangerous","Band":"David Guetta","Nettoeinzelpreis":"1.99"},
{"Produktid":"02","Titel":"Sun goes down","Band":"Robin Schulz","Nettoeinzelpreis":"1.99"},
{"Produktid":"03","Titel":"Fade out lines","Band":"The Avener","Nettoeinzelpreis":"1.99"},
{"Produktid":"04","Titel":"Walk","Band":"Kwabs","Nettoeinzelpreis":"1.99"},
{"Produktid":"05","Titel":"Blame","Band":"Calvin Harris","Nettoeinzelpreis":"1.99"},
{"Produktid":"06","Titel":"Geronimo","Band":"Sheppard","Nettoeinzelpreis":"1.99"},
{"Produktid":"07","Titel":"Animals","Band":"Maroon 5","Nettoeinzelpreis":"1.99"},
{"Produktid":"08","Titel":"What are you waiting for?","Band":"Nickelback","Nettoeinzelpreis":"1.99"},
{"Produktid":"09","Titel":"Shake it off","Band":"Taylor Swift","Nettoeinzelpreis":"1.99"},
{"Produktid":"10","Titel":"Chandelier","Band":"Sia","Nettoeinzelpreis":"1.99"} ];
for (var i = 0; i < titelliste.length; i++) {
var select = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = titelliste[i].Titel;
option.value = titelliste[i].Produktid;
select.add(option);
}
}
答案 0 :(得分:3)
您也可以使用map()
。函数的参数val
是包含Produktid
和Titel
的对象。所以你可以使用map()
函数。这是一个例子:
var titelliste= [
{"Produktid":"01","Titel":"Dangerous","Band":"David Guetta","Nettoeinzelpreis":"1.99"},
{"Produktid":"02","Titel":"Sun goes down","Band":"Robin Schulz","Nettoeinzelpreis":"1.99"},
{"Produktid":"03","Titel":"Fade out lines","Band":"The Avener","Nettoeinzelpreis":"1.99"},
{"Produktid":"04","Titel":"Walk","Band":"Kwabs","Nettoeinzelpreis":"1.99"},
{"Produktid":"05","Titel":"Blame","Band":"Calvin Harris","Nettoeinzelpreis":"1.99"},
{"Produktid":"06","Titel":"Geronimo","Band":"Sheppard","Nettoeinzelpreis":"1.99"},
{"Produktid":"07","Titel":"Animals","Band":"Maroon 5","Nettoeinzelpreis":"1.99"},
{"Produktid":"08","Titel":"What are you waiting for?","Band":"Nickelback","Nettoeinzelpreis":"1.99"},
{"Produktid":"09","Titel":"Shake it off","Band":"Taylor Swift","Nettoeinzelpreis":"1.99"},
{"Produktid":"10","Titel":"Chandelier","Band":"Sia","Nettoeinzelpreis":"1.99"} ];
var options = titelliste.map(function(val, ind){
return $("<option></option>").val(val.Produktid).html(val.Titel);
});
$('#mySelect').append(options);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<select id="mySelect"></select>
&#13;
<强>参考:强>
另请参阅另一个迭代 - each()
答案 1 :(得分:3)
这是一个纯JavaScript选项:
var titelliste = [
{ "Produktid": "01", "Titel": "Dangerous", "Band": "David Guetta", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "02", "Titel": "Sun goes down", "Band": "Robin Schulz", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "03", "Titel": "Fade out lines", "Band": "The Avener", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "04", "Titel": "Walk", "Band": "Kwabs", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "05", "Titel": "Blame", "Band": "Calvin Harris", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "06", "Titel": "Geronimo", "Band": "Sheppard", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "07", "Titel": "Animals", "Band": "Maroon 5", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "08", "Titel": "What are you waiting for?", "Band": "Nickelback", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "09", "Titel": "Shake it off", "Band": "Taylor Swift", "Nettoeinzelpreis": "1.99" },
{ "Produktid": "10", "Titel": "Chandelier", "Band": "Sia", "Nettoeinzelpreis": "1.99" }
];
for (var i = 0; i < titelliste.length; i++) {
var select = document.getElementById("Select");
var option = document.createElement("option");
option.text = titelliste[i].Titel;
option.value = titelliste[i].Produktid;
select.add(option);
}
&#13;
<select id="Select" name="Titel"></select>
&#13;
从您的代码中可以明显看出,您并不熟悉jQuery的工作原理,因此我建议您坚持使用纯JavaScript,直到您完全理解语言本身为止。
我在这里做的很简单:
for (var i = 0; i < titelliste.length; i++) {
var select = document.getElementById("Select");
var option = document.createElement("option");
option.text = titelliste[i].Titel;
option.value = titelliste[i].Produktid;
select.add(option);
}
首先,我创建一个for
循环来循环遍历titelliste
数组中包含的每个JSON对象。
然后,在每次迭代时:
select
元素。option
元素text
属性分配给JSON对象中的Titel
值value
属性分配给JSON对象中的Produktid
值select
元素我希望这可以帮助您更多地了解代码中的内容。
答案 2 :(得分:0)
void main的回答非常简洁明了。
不需要使用eval,因为变量已经是一个对象数组。 考虑到你要为它添加元素,你的选择框应该是空的。
这是一个仍然使用javascript for循环的jsfiddle:
<select id="mySelect"></select>
function initSelBox_Product() {
var titelliste = [
{"Produktid":"01","Titel":"Dangerous","Band":"David Guetta","Nettoeinzelpreis":"1.99"},
{"Produktid":"02","Titel":"Sun goes down","Band":"Robin Schulz","Nettoeinzelpreis":"1.99"},
{"Produktid":"03","Titel":"Fade out lines","Band":"The Avener","Nettoeinzelpreis":"1.99"},
{"Produktid":"04","Titel":"Walk","Band":"Kwabs","Nettoeinzelpreis":"1.99"},
{"Produktid":"05","Titel":"Blame","Band":"Calvin Harris","Nettoeinzelpreis":"1.99"},
{"Produktid":"06","Titel":"Geronimo","Band":"Sheppard","Nettoeinzelpreis":"1.99"},
{"Produktid":"07","Titel":"Animals","Band":"Maroon 5","Nettoeinzelpreis":"1.99"},
{"Produktid":"08","Titel":"What are you waiting for?","Band":"Nickelback","Nettoeinzelpreis":"1.99"},
{"Produktid":"09","Titel":"Shake it off","Band":"Taylor Swift","Nettoeinzelpreis":"1.99"},
{"Produktid":"10","Titel":"Chandelier","Band":"Sia","Nettoeinzelpreis":"1.99"} ];
for (var key in titelliste)
{
var option = titelliste[key];
var newOption = $('<option/>');
newOption.val(option.Produktid);
newOption.text(option.Titel);
$('#mySelect').append(newOption);
}
}
initSelBox_Product();
答案 3 :(得分:0)
您的代码中需要更改一些内容。
<option>
代码没有&#34;文字&#34;属性。在jQuery中,您应该使用$('.selector').text()
来更改标记的内部文本<select>
标记的ID属性与您在$('#mySelect').append(newOption);
中指定的属性不同(您的选择是ID =&#34;选择&#34;不是&#34; mySelect&#34)这是一个有效的fiddle。