如何在jQuery中搜索JSON
How to search JSON in jQuery
你好,我有 3 个包含 1,2,3 的 DIV
<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"
}]
我希望当我将鼠标悬停在 DIV 上时检查它们的值。
如果 DIV 包含1
显示 "FactoreId:1": "FactoreItems:a, b, c, d, e"
的因子项,如果它包含2
显示"FactoreId:2": "FactoreItems:g,f"
的因子项,依此类推....
您发布的 JSON 不正确,请将其修复为"FactoreId" : num, "FactoreItems": "string"
.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);
});
演示
解释
因子:是带有 JSON 的数组
$(this).text() -1
返回divs 编号和 -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
相关文章:
- 在禁用ng的情况下搜索JSON对象(AngularJS)
- 使用ng类(AngularJS)搜索JSON对象
- 自动完成功能,用于按属性搜索 json 对象
- 如何搜索 json 结构
- 如何在jQuery中搜索JSON
- 搜索 JSON 数组
- 根据变量搜索 JSON 数组
- 递归搜索 JSON 并删除某些子对象
- 使用jQuery提高搜索JSON对象的性能
- 将Multiple Var输入函数,搜索JSON数组,并将Multiple Array Val作为输出接收
- 用于搜索JSON数据的自定义筛选器
- 在JavaScript中搜索Json
- 如何在JavaScript中搜索JSON数组中的特定参数
- 使用jQuery循环浏览twitter搜索JSON
- 搜索JSON以查找特定值
- TypeAhead和Bloodhound只搜索JSON数组中的第一个单词
- 使用JQuery搜索JSON对象
- 使用从下拉菜单输入的用户日期搜索JSON
- 在ember.js中搜索JSON数据
- 二进制搜索JSON对象