我試圖創建一個for()
循環與已被解碼的JSON數據,問題是將我的循環變量(在這種情況下爲i
)添加到我引用的元素的末尾。對JSON對象/數組元素使用for循環?
例如,我的JSON結構,傾倒:
{
"url": "http://www.example.com",
"rid": 1,
"rname": "Apple iPhone 4 Rotation",
"rmin": 90,
"rmax": 150,
"blank": 0,
"geo_country_0": "GB",
"geo_url_0": "http://en",
"geo_country_1": "FR",
"geo_url_1": "http://fr",
"geo_country_2": "ES",
"geo_url_2": "http://es"
}
在geo_country_
的情況下和與geo_url_
我需要附加的數,在這種情況下,循環變量。
我的for循環的實際,爲了更好的理解:
for(i = 1; i < count; i++) {
$('#geo-location').append('<div id="glt_' + i + '" class="clone"><select name="g_country['+i+']" class="glt-input-c">' + options + '</select><input type="text" name="g_url[' + i + ']" class="glt-input-c" value="' + data.geo_url_i+ '"/></div>');
$('select[name="g_country['+i+']"').find('option[value="' + data.geo_country_i+ '"]').attr('selected','selected');
}
到目前爲止,我已經試過:
- 罩住的
i
像[i]
。 - 轉義爲
\i
。 (這可能是一個愚蠢的想法)。 - 添加預定義
+
like,+ data.geo_country + i +
。
我需要做些什麼才能讓i
正確解釋?
任何幫助將不勝感激!
您是否在嘗試引用像'data ['geo_country _'+ i]''data.geo_country_?'? – dgilland