我挣扎着两个循环。我想要实现的是匹配相应的对象来纠正每个迭代。两个嵌套对象和div选择器的每个循环
有我的小提琴。在每个div中都有相同的输出,它应该是1,2,3而不是3,3,3。
jQuery('.get-total-numbers').each(function(i, e) {
var $el = jQuery(e);
var json = '["1","2","3"]';
var arr = $.parseJSON(json);
myjson = json;
json2 = JSON.parse(json);
jQuery.each(json2, function(index, e) {
console.log(e);
$el.text(e);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="get-total-numbers">
</div>
<div class="get-total-numbers">
</div>
<div class="get-total-numbers">
</div>
谢谢你的编辑@Barmar – Matt
你没有做任何事情来匹配相应的元素。内循环只是贯穿整个数组,用数组中的每个项目替换当前的DIV。所以当它完成时,DIV包含数组的最后一个元素。 – Barmar
以为这是一个很好的开始。我想我的输出为第一格为1,第二格为2等。 – Matt