2017-06-17 72 views
0

我遇到此函數的問題。當我將對象推入數組時,它是可以的,但是當我檢索存儲的對象時,我在每個對象中都有相同的值。將對象推入數組後,數組中的每個對象都具有相同的值

function saveDetails() { 
    var details = []; 
    var productDetails = {}; 
    var id = $('#ddlDeliveryBoy option:selected').val(); 
    var count = $('#tbCustomer tbody tr').length; 
    for (var index = 0; index < count; index++) { 
    var tr = $('#tbCustomer tbody tr')[index]; 
    var countTD = $(tr).children('td').length - 4; 
    for (var j = 3; j < (countTD + 4); j++) { 
     var td = $(tr).children('td')[j]; 
     var txt = $(td).find('input[type="text"]'); 
     if ($.isNumeric(txt.val())) { 
     productDetails.CustomerID = $(tr).data('id'); 
     productDetails.ProductID = $(td).data('id'); 
     productDetails.Quantity = parseFloat(txt.val()).toFixed(2); 
     details.push(productDetails); 
     } 
    } 
    } 
    console.log(details); 
} 
+0

'$( '#tbCustomer TBODY TR')。EQ(指數)'試着用'EQ()' – prasanth

回答

2

你需要移動產品詳細內如果循環初始化。 這是代碼。

function saveDetails() { 
    var details = []; 
    var id = $('#ddlDeliveryBoy option:selected').val(); 
    var count = $('#tbCustomer tbody tr').length; 
    for (var index = 0; index < count; index++) { 
    var tr = $('#tbCustomer tbody tr')[index]; 
    var countTD = $(tr).children('td').length - 4; 
    for (var j = 3; j < (countTD + 4); j++) { 
     var td = $(tr).children('td')[j]; 
     var txt = $(td).find('input[type="text"]'); 
     if ($.isNumeric(txt.val())) { 
     let productDetails = {}; 
     productDetails.CustomerID = $(tr).data('id'); 
     productDetails.ProductID = $(td).data('id'); 
     productDetails.Quantity = parseFloat(txt.val()).toFixed(2); 
     details.push(productDetails); 
     } 
    } 
    } 
    console.log(details); 
} 
+0

感謝#Raj .......... –

相關問題