2013-10-07 62 views
0

你好我有一个包含1,2,3如何搜索JSON jQuery中

<div>1</div> 
<div>2</div> 
<div>3</div> 

,并有此JSON

[{ 
    "FactoreId:1": "FactoreItems:a, b, c, d, e" 
}, { 
    "FactoreId:2": "FactoreItems:g,f" 
}, { 
    "FactoreId:3": "FactoreItems:i, k, h" 
}] 

我想3个的DIV,当我将鼠标悬停在资料核实,他们的值被检查。

如果DIV包含1显示的"FactoreId:1": "FactoreItems:a, b, c, d, e"的FactoreItems,如果它包含2显示的"FactoreId:2": "FactoreItems:g,f"的FactoreItems等等....

+1

可能重复? http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript –

+0

请详细解释一下这个问题。 'factoreid 1 factoreitems'是什么意思? 此外,如果您使用'div'发布了HTML,这将会很有帮助。 – Bryce

+0

你从这种形式的其他来源得到这个JSON吗?如果结构是由您创建的,我会将该数组更改为Object,并将数字键仅更改为更简单和更高性能的访问。 –

回答

1

您发布不正确,固定它如此 “FactoreId” 的JSON:NUM, “FactoreItems”: “串”

HTML

<div>1</div> 
<div>2</div> 
<div>3</div> 

的jQuery

//Fixed JSON 
var factore = [{ 
     "FactoreId": 1, 
     "FactoreItems": "a, b, c, d, e" 
    }, { 
     "FactoreId": 2, 
     "FactoreItems": "g,f" 
    }, { 
     "FactoreId": 3, 
     "FactoreItems": "i, k, h" 
    }] 
    $('div').on('mouseover', function() { 
     $(this).text("FactoreID : "+factore[$(this).text() -1].FactoreId +"FactoreItems"+ factore[$(this).text()-1].FactoreItems); 
    }); 

DEMO

说明

factore:与JSON阵列

$(this).text() -1返回的div数目和-1,因为阵列从0

factore[$(this).text() - 1] 
// if you mouseover div with text one the result will be factore[0] 
// then use .FactoreId to get id value 
开始
+0

你可以告诉我关于$('div')。on('mouseover',function(){ $(this).text(“FactoreID:”+ factore [$(this).text() - 1] .FactoreId +“FactoreItems”+ factore [$(this).text() - 1] .FactoreItems); }); ? – user2781200

+0

@ user2781200更新了解释 – Anton

+1

谢谢alottttttttttttt – user2781200