我有一个像这样的jQuery对象(计数):
Object {men: 299, womens: 175, children: 173}
我只想检索值以在span中显示它(数据过滤器属性与对象键相同)
$( "a.type_select" ).each(function() {
var theType = $(this).attr('data-filter');
var theCounter = counts.theType;
$(this).children('span.count').html(' ('+theCounter+')');
});
我该怎么做?
谢谢!
答案 0 :(得分:1)
counts.theType
访问对象theType
的密钥counts
。
重要的是要知道对象密钥也可以通过字符串访问。你只需要使用支架。例如:
counts['men']; // 299
话虽如此,您可以将变量传递给括号:
counts[theType]
这将返回你想要的东西。