晚上好,
我有以下代码
$( document ).ready(function() {
$.get( "php/get_ratings.php")
.done(function(data) {
$("#newrow").html('');
$("#list_loc").html('');
var results = jQuery.parseJSON(data);
$.each(results, function(i, value) {
var newrow = $("<div />", {
id : "new"+results[i].id
});
var newLoc = $("<div />", {
id: "loc"+results[i].id,
text: results[i].city
});
$("#newrow").append(newrow);
$("#list_loc").append(newLoc);
$('#list_loc').appendTo('#newrow');
})
});
});html
<div class="container">
<div class="row list">
<div id="newrow">
<div class="row">
<div class="col-md-12">
<div id="list_loc">
</div>
</div>
</div>
</div>
</div>
</div>我试图实现的是创建两个动态div,然后将一个div插入另一个div,但是由于某种原因,我只得到了我的"newrow“div。有人能解释一下我做错了什么吗?
提前谢谢你,
D
我期待最后的html看起来是什么样子
<div class="list">
<div id="row1">
<div id="loc1">
</div>
<div id="row2">
<div id="loc2">
</div>
</div>
</div>发布于 2015-07-14 17:38:42
试试这个:
$(document).ready(function () {
$.get("php/get_ratings.php")
.done(function (data) {
$("#newrow").html('');
// $("#list_loc").html(''); No point, you already cleared it in the line above
var results = jQuery.parseJSON(data);
$.each(results, function (i, value) {
var newrow = $("<div />", {
id: "new" + results[i].id
}).append( //new loc is appeneded directly to new row
$("<div />",
{
id: "loc" + results[i].id,
text: results[i].city
})
);
$("#newrow").append(newrow);
});
});
});https://stackoverflow.com/questions/31412735
复制相似问题