我正在处理这段代码:
$.post(Routing.generate('vincularFabricanteModeloMarca'), {
fabricantesMarcaModelo: fabricantesMarcaModelo.serializeArray(),
paresMarcaModelo: paresMarcaModelo.serializeArray()
}, 'json').done(function (data, textStatus, jqXHR) {
if (data.success) {
console.log(data.ent);
$.each(data.ent, function (k, v) {
var countries = '';
var maker = '';
var makerId = null;
console.log(v.paisesFabricanteModeloMarca);
$.each(v.paisesFabricanteModeloMarca, function (l, w) {
$.each(w, function (x, z) {
countries += (countries == '' ? '' : ', ') + z.nombrepais;
maker = z.nombrefabricantedistribuidor;
makerId = z.idfabricantedistribuidor;
});
var btn = '<button data-rel-id="' + makerId + '" class="btn btn-default btn-sm btn-fabricante"><i class="fa fa-close fx"></i> ' + maker + ' (' + countries + ')</button>';
$('td[data-modelomarcaproductofabricantes="' + v.idModeloMarca + '"]').append(btn);
});
});
}
}).fail(function () { });
如何在此结果集上获取此Javascript对象的key
值,以便在此行中找到正确的td
:
$('td[data-modelomarcaproductofabricantes="' + (here) + '"]').append(btn);
(这里)应该是每个循环的关键,怎么样?请参阅下面的图片了解测试结果集,任何建议?
的console.log(data.ent)
这是服务器端响应为JSON
答案 0 :(得分:1)
查看你的代码和JSON,我觉得你错过了一个循环,
JSON结构似乎是Array - &gt; Object - &gt; Object - &gt; Array,
你的循环就像数组 - &gt;对象 - &gt;数组,
检查这是否有效,
$.each(Object.keys(v), function(k1,v1){
console.log(v[v1].paisesFabricanteModeloMarca);
});
答案 1 :(得分:0)
所以在 @ mido22 建议后,我重写了自己的代码并得到了这个:
$.each(data.ent, function (k, v) {
var countries = '',
maker = '',
makerId = null;
$.each(v, function (l, w) {
$.each(w.paisesFabricanteModeloMarca, function (i, j) {
countries += (countries == '' ? '' : ', ') + j.nombrepais;
maker = j.nombrefabricantedistribuidor;
makerId = j.idfabricantedistribuidor;
});
var btn = '<button data-rel-id="' + makerId + '" class="btn btn-default btn-sm btn-fabricante"><i class="fa fa-close fx"></i> ' + maker + ' (' + countries + ')</button>';
$('td[data-modelomarcaproductofabricantes="' + l + '"]').append(btn);
});
});
功能齐全且显然对我有用!希望这能帮到别人。快乐编码!!