admin管理员组文章数量:1327661
i am trying to create a select box dynamically with data that is within an array, I tried watching some JSON tutorials, yet having some trouble still.
var clothes = [
Red Dress:"reddress.png",
Blue Dress:"bluedress.png",
Black Hair Pin:"hairpin.png"
];
var select = '<select id="clothing_options">';
for(var i=0;i<clothes.length;i++)
{
select +='<option value="'+secondPart[i]+'">'+firstPart[i]+'</option>';
}
$('#select_box_wrapper').append(select+'</select>');
$('#clothing_options').change(function() {
var image_src = $(this).val();
$('#clothing_image').attr('src','/'+image_src);
});
as you can see code is not fully functioning because it is not written correctly. How do I get the data for value from the second part and the option text from the first part? basically html should look like this
<select id="clothing_options">
<option value="reddress.png">Red Dress</option>
<option value="bluedress.png">Blue Dress</option>
<option value="hairpin.png">Black Hair Pin</option>
</select>
thanks for any explanations or suggestions. Just want this code to work, as I am just doing these codes for lessons for myself
i am trying to create a select box dynamically with data that is within an array, I tried watching some JSON tutorials, yet having some trouble still.
var clothes = [
Red Dress:"reddress.png",
Blue Dress:"bluedress.png",
Black Hair Pin:"hairpin.png"
];
var select = '<select id="clothing_options">';
for(var i=0;i<clothes.length;i++)
{
select +='<option value="'+secondPart[i]+'">'+firstPart[i]+'</option>';
}
$('#select_box_wrapper').append(select+'</select>');
$('#clothing_options').change(function() {
var image_src = $(this).val();
$('#clothing_image').attr('src','http://www.imagehosting./'+image_src);
});
as you can see code is not fully functioning because it is not written correctly. How do I get the data for value from the second part and the option text from the first part? basically html should look like this
<select id="clothing_options">
<option value="reddress.png">Red Dress</option>
<option value="bluedress.png">Blue Dress</option>
<option value="hairpin.png">Black Hair Pin</option>
</select>
thanks for any explanations or suggestions. Just want this code to work, as I am just doing these codes for lessons for myself
Share Improve this question edited May 7, 2013 at 2:22 Þaw 2,0574 gold badges22 silver badges40 bronze badges asked May 7, 2013 at 1:57 EasyBBEasyBB 6,5849 gold badges50 silver badges81 bronze badges2 Answers
Reset to default 3You could change your array to a JSON object..
var clothes = {
"Red Dress":"reddress.png",
"Blue Dress":"bluedress.png",
"Black Hair Pin":"hairpin.png"
};
and then iteration bees easier..
for(var item in clothes)
{
$('<option value="'+item+'">'+clothes[item]+'</option>').appendTo('#clothing_options');
}
Here's the HTML:
<div id="select_box_wrapper">
<select id="clothing_options"></select>
</div>
Demo
First problem:
var clothes = {
Red_Dress:"reddress.png",
Blue_Dress:"bluedress.png",
Black_Hair_Pin:"hairpin.png"
};
You can't have spaces in identifiers.
Second, to loop through an object:
for (var key in clothes)
{
select +='<option value="'+clothes[key]+'">'+key+'</option>';
}
Of course this has the undesired effect of showing 'Red_Dress' in the select box.
var clothes = {
"Red Dress":"reddress.png",
"Blue Dress":"bluedress.png",
"Black Hair Pin":"hairpin.png"
};
That will fix it.
本文标签: javascriptCreating a select box dynamically with data from arrayStack Overflow
版权声明:本文标题:javascript - Creating a select box dynamically with data from array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742229730a2436999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论