尝试填充$ .each循环中的文本字段,我可以从JSAON数组中获取单独的对象,但是首先要获取所有对象的计数并在$ .each中创建循环是具有挑战性的:请参见JSFiddle第253行。 / p>
console.log向invoice
容器显示JSON,但只需要为循环计数。我可以从第260行的过滤器中获得计数,但是.each不能与for
循环一起使用。
https://jsfiddle.net/jeffbarclay/2r3unLsc/
$('.add-invoice').on('click', function () {
$("#invoice_div").css("display", "block");
$.ajax({
url: '/echo/json/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: { json: JSON.stringify(myjson) },
success: function (data) {
var result = [];
$.each(data, function (i, e) {
var matchingItems = $.grep(result, function (item) {
return item.customer === e.customer && item.cust_id === e.cust_id;
});
if (matchingItems.length === 0) {
result.push(e);
}
});
populateSelectBoxes($('#invoice_div #ddinvoice'), result);
function populateSelectBoxes($select, result) {
var invoices = [];
$.each(result, function () {
invoices.push('<li data-value="' + this.cust_id + '">' + this.customer + ' : ' + this.invoice + '</li>');
});
$select.append(invoices.join(''));
}
function populateTableRow(data, selectedProductAutonum) {
var invoices;
//var cp = 0;
//var customerProducts;
$.each(data, function (i, e) {
if (this.cust_id == selectedProductAutonum) {
invoices = this;
custProducts = data.filter(({ cust_id }) => cust_id === selectedProductAutonum);
//alert(JSON.stringify(invoices));
//console.log("custProducts.length: = "+ custProducts.length + "\n" + "custProducts string: "+ JSON.stringify(custProducts));
//console.log("cust_id is: " + this.cust_id);
//console.log("invoices = " + JSON.stringify(invoices));
$.each(invoices, function (i, ee) {
console.log(ee);
console.log("new invoice loop: " + JSON.stringify(invoices));
$(".item-row:last").after(
'<tr class="item-row"><td class="item-name"><div class="delete-wpr"><textarea form ="testinsert" name="item_name[cp]">Item Name</textarea><a class="delete" href="javascript:;" title="Remove row">X</a><a class="add-product" href="javascript:;" title="Add Product">A</a></div></td><td class="description"><textarea form ="testinsert" name="item_desc[cp]">Description</textarea></td><td><textarea class="cost" form ="testinsert" name="item_cost[cp]">$0</textarea></td><td><textarea class="qty" form ="testinsert" name="item_qty[cp]">0</textarea></td><td><span class="price" form ="testinsert" name="item_price[cp]">$0</span></td></tr>');
if ($(".delete").length > 0) $(".delete").show();
bind();
$('#address-title').val(invoices.customer);
$('#address-one').val(invoices.address);
$('#invoice_num').val(invoices.invoice);
$('#paid').val(invoices.paid);
$('#owed').val(invoices.sales);
$('#auto_num').val(invoices.autonum);
$('[name="item_name[cp]"]').val(invoices.product);
$('[name="item_desc[cp]"]').val(invoices.description);
$('[name="item_cost[cp]"]').val(invoices.cost);
$('[name="item_qty[cp]"]').val(invoices.quantity);
$('[name="item_price[cp]"]').val(invoices.price);
});
}
});
//alert(invoices.invoice + '\n'+ invoices.customer + '\n'+ invoices.name + '\n'+invoices.product + '\n'+ invoices.description + '\n'+ invoices.cost + '\n'+ invoices.quantity + '\n'+ invoices.price);