Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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中使用select从json获取数据_Javascript_Json - Fatal编程技术网

无法在javascript中使用select从json获取数据

无法在javascript中使用select从json获取数据,javascript,json,Javascript,Json,我是Json和javascript的新手。我想要的是一个带有选项的select标记,因此通过选择选项,特定对象将从JSON中获取并显示,我使用了嵌套JSON data.json: var data = { "abc":{ "color":["rgb(4,45,97)","rgb(255,122,0)"], "font":"Interface Thin", "logo":"<img src='logo.

我是Json和javascript的新手。我想要的是一个带有选项的select标记,因此通过选择选项,特定对象将从JSON中获取并显示,我使用了嵌套JSON

data.json:

var data = {

        "abc":{
            "color":["rgb(4,45,97)","rgb(255,122,0)"],
            "font":"Interface Thin",
            "logo":"<img src='logo.png' width='200' height='150'>"
        }


    }
var数据={
“abc”:{
“颜色”:[“rgb(4,45,97)”,“rgb(255122,0)”,
“字体”:“界面精简”,
“徽标”:”
}
}
index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="data.json"></script>
</head>

<body>
<!--document.getElementById('color').innerHTML += '<div style="border:1px solid black;padding:2px;width:100px;background-color:'+data.AllenandHanburys[j]+';height:100px"></div>'-->

<p id="demo"></p>
<p id="color"></p>
<p id="logo"></p>

<select id="brand" onChange="run()"> 
    <option>Select Option</option>
    <option value="abc">abc</option>
</select>
</body>

<script>
var i;
function run() {
var brand_name=document.getElementById('brand').value;//
for(x in data)
{
    if(x==brand_name)
    {
        for(j in data.brand_name)
            {
                document.getElementById('demo').innerHTML +=j+":"+data.abc[j]+"<br>";
                if(j=='color')
                    {
                            for(i=0;i<data.abc.color.length;i++)
                            {
                                document.getElementById('color').innerHTML +='<div style="border:1px solid black;padding:2px;width:100px;background-color:'+data.abc.color[i]+';height:100px"></div>'+"<br>";
                            }


                    }



            }
    }
}
}
</script>

</html>

选择选项 abc var i; 函数运行(){ var brand_name=document.getElementById('brand').value// 对于(数据中的x) { 如果(x==品牌名称) { 用于(数据中的j.品牌名称) { document.getElementById('demo').innerHTML+=j+“:“+data.abc[j]+”
”; 如果(j=='color') { 对于(i=0;i这条线是错误的

for(j in data.brand_name) //because `brand_name` property is not exist in the object
换成

for(j in data[brand_name]) //data[brand_name] will parsed as data['abc'] which is same as data.abc
这是演示

var数据={
“abc”:{
“颜色”:[“rgb(4,45,97)”,“rgb(255122,0)”,
“字体”:“界面精简”,
“徽标”:”
}
}
var i;
函数运行(){
var brand_name=document.getElementById('brand').value//
对于(数据中的x)
{
如果(x==品牌名称)
{
对于(j in数据[品牌名称])
{
document.getElementById('demo').innerHTML+=j+“:“+data.abc[j]+”
”; 如果(j=='color') {
对于(i=0;i)您可能应该显示一些您尝试过的代码。这里是我上传代码的链接:更新了我的答案。请查看为我工作的演示。不要忘记将此标记为答案,也可以进行投票。