使用jquery基于数组中的对象值设置列表项的宽度

时间:2015-01-14 11:17:38

标签: jquery arrays object

我有一个包含一组对象的数组。

var country_data = [
{"country_id":1,"country":"Luxembourg","local_wage":"407","wage":"489","exchange":"0.98"},
{"country_id":2,"country":"Norway","local_wage":"3200","wage":"378","exchange":"9.57"},
{"country_id":3,"country":"Austria","local_wage":"290","wage":"337","exchange":"0.87"},
{"country_id":4,"country":"United States","local_wage":"363","wage":"363","exchange":"1"},
{"country_id":5,"country":"United Kingdom","local_wage":"284","wage":"365","exchange":"0.68"}
];

我还有一个for循环,它从每个对象中检索第三个值(工资):

for (var i=0; i<country_data.length; i++){
    var wage = country_data[i].wage;
    $('.countries.country_'+i).css('width', wage+'px');

我想使用.css,width属性将该图的值附加到相应列表元素的宽度。

<ul class="countries">
<li class="country_1">test</li>
<li class="country_2">test</li>
<li class="country_3">test</li>
<li class="country_4">test</li>
<li class="country_5">test</li>
</ul>

我可以让它循环遍历数组但不附加到相应的列表元素。任何帮助表示感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

你的选择器错了,应该是

$('.countries .country_' + i)

$('.country_' + i)

你的i参数也是错误的。它与0相比你应该最有可能使用country_data[i].country_id