-1
我有多個所有者的json列表。每個擁有者都有自己的定時器列表。如何通過json循環並追加內部追加
我想遍歷所有者,並在已經附加的所有者中追加定時器。
我該怎麼做?
編輯:更改#定時器列表#業主名單
這裏是我的JSON:
[
{
"ownerid": 1,
"name": "Owner 1",
"timers": [
{
"duration": "00:57:04.521586",
"started": "2015-10-02T12:28:46+00:00",
"completed_date": null,
"paused_date": null,
"objectid": 158,
"paused": false,
"completed": false,
"resume_date": null
},
{
"duration": "00:20:00",
"started": null,
"completed_date": null,
"paused_date": null,
"objectid": 172,
"paused": false,
"completed": false,
"resume_date": null
}
]
},
{
"ownerid": 2,
"name": "Owner 2",
"timers": [
{
"duration": "10:00:00",
"started": null,
"completed_date": null,
"paused_date": null,
"objectid": 173,
"paused": false,
"completed": false,
"resume_date": null
},
{
"duration": "00:00:55",
"started": null,
"completed_date": null,
"paused_date": null,
"objectid": 176,
"paused": false,
"completed": false,
"resume_date": null
}
]
}
]
我的JS:
$.each(data, function(index, element) {
console.log(element.ownername);
$('#owner-list').append("<div id='timer_owner_" + element.ownerid + "'><h5>" + element.ownername + "</h5></div>");
$.each(element.timers, function(index, element) {
console.log(element);
});
});
我如何追加到timer_owner_" + element.ownerid + "
?
編輯2:
這裏是我想要的HTML:
<div id="owner-list">
<div id="timer_owner_1">
<h5>Owner 1</h5>
<ul class="timer-list">
<li>Timer 1</li>
<li>Timer 2</li>
</ul>
</div>
<div id="timer_owner_2">
<h5>Owner 2</h5>
<ul class="timer-list">
<li>Timer 3</li>
<li>Timer 4</li>
</ul>
</div>
</div>
'#timer-list'是我希望所有所有者被追加到的容器。我需要追加每個計時器列表到特定的所有者div'#timer_owner_「+ element.ownerid +」'我改變了我的問題,所以它會更清晰一點。 –
baaah ..試試這個代碼,你會看到結果:°) – Mimouni
解釋:當我們用id =#timer_owner_「+ element.ownerid打開div時,我們在這個例子中添加其他信息:span中的持續時間。結束我們關閉div – Mimouni