[英]Trouble Making array in javascript
我在使用javascript制作數組時遇到了麻煩。 首先,我是這樣做的
for(var i=1; i <= rowCount-1; i++)
{
product[i-1] = [{
'product_id' : $('#product_id' + i).val(),
'name' : $('#item' + i).val(),
'model' : $('#model' + i).val(),
'reward' : $('#reward' +i).val(),
'subtract' : $('#subtract' + i).val(),
'minimum' : $('#minimum' + i).val(),
'shipping' : $('#shipping' + i).val(),
'tax_class_id' : $('#tax_class_id' + i).val(),
'weight' : $('#weight' + i).val(),
'quantity' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').val(),
'price' : $('table.order-table tbody tr[id=\''+ i +'\']').find('td.price').html(),
'total' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').parent().parent().find('td.total').html()
}];
}
在ajax帖子中將其作為參數傳遞后,我可以看到它將其傳遞為
product[0][0][minimum]
product[0][0][model] 326
product[0][0][name] apple mac power
product[0][0][price] 100.0000
product[0][0][product_id] 50
product[0][0][quantity] 5
product[0][0][reward] 0
product[0][0][shipping] 1
product[0][0][subtract] 1
product[0][0][tax_class_i... 0
product[0][0][total] 500
product[0][0][weight] 0.00000000
product[1][0][minimum]
product[1][0][model] 326
product[1][0][name] apple mac power
product[1][0][price] 100.0000
product[1][0][product_id] 50
product[1][0][quantity] 7
product[1][0][reward] 0
product[1][0][shipping] 1
product[1][0][subtract] 1
product[1][0][tax_class_i... 0
product[1][0][total] 700
但是我想要這樣的東西
product[0][name] = "apple mac power"
所以我把我的代碼改成了這個
for(var i=1; i <= rowCount-1; i++)
{
product = [{
'product_id' : $('#product_id' + i).val(),
'name' : $('#item' + i).val(),
'model' : $('#model' + i).val(),
'reward' : $('#reward' +i).val(),
'subtract' : $('#subtract' + i).val(),
'minimum' : $('#minimum' + i).val(),
'shipping' : $('#shipping' + i).val(),
'tax_class_id' : $('#tax_class_id' + i).val(),
'weight' : $('#weight' + i).val(),
'quantity' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').val(),
'price' : $('table.order-table tbody tr[id=\''+ i +'\']').find('td.price').html(),
'total' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').parent().parent().find('td.total').html()
}];
}
因此,在執行此操作之后,無論我有多少rowCount,它都只顯示1行數組
product[0][minimum]
product[0][model] 326
product[0][name] apple mac power
product[0][price] 100.0000
product[0][product_id] 50
product[0][quantity] 7
product[0][reward] 0
product[0][shipping] 1
product[0][subtract] 1
product[0][tax_class_i... 0
product[0][total] 700
誰能幫我。?
提前致謝..
您正在創建一個數組數組。 取出第二個數組,如下所示:
for(var i=1; i <= rowCount-1; i++)
{
product[i-1] = { // Removed array open
'product_id' : $('#product_id' + i).val(),
'name' : $('#item' + i).val(),
'model' : $('#model' + i).val(),
'reward' : $('#reward' +i).val(),
'subtract' : $('#subtract' + i).val(),
'minimum' : $('#minimum' + i).val(),
'shipping' : $('#shipping' + i).val(),
'tax_class_id' : $('#tax_class_id' + i).val(),
'weight' : $('#weight' + i).val(),
'quantity' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').val(),
'price' : $('table.order-table tbody tr[id=\''+ i +'\']').find('td.price').html(),
'total' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').parent().parent().find('td.total').html()
}; // Removed array close
}
在JavaScript中,[]創建一個數組,因此[{object}]創建一個單元格數組。
另請注意,product [0] [“name”]與product [0] .name相同 - 在JavaScript中,屬性也可用於索引語法。 product是您的數組,數組中的每個單元格都是一個對象,它具有包括name在內的屬性。
在您的第一個版本中,您將數組分配給數組,因此它顯示為2d數組。 在第二個版本中,您始終將產品變量分配給新值,因此它只包含一個產品信息。 因此,您可以通過刪除[和]來更新您的第一個版本
for(var i=1; i <= rowCount-1; i++)
{
product[i-1] = {
'product_id' : $('#product_id' + i).val(),
'name' : $('#item' + i).val(),
'model' : $('#model' + i).val(),
'reward' : $('#reward' +i).val(),
'subtract' : $('#subtract' + i).val(),
'minimum' : $('#minimum' + i).val(),
'shipping' : $('#shipping' + i).val(),
'tax_class_id' : $('#tax_class_id' + i).val(),
'weight' : $('#weight' + i).val(),
'quantity' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').val(),
'price' : $('table.order-table tbody tr[id=\''+ i +'\']').find('td.price').html(),
'total' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').parent().parent().find('td.total').html()
};
}
或者你可以修改你的第二個版本
for(var i=1; i <= rowCount-1; i++)
{
product.push({
'product_id' : $('#product_id' + i).val(),
'name' : $('#item' + i).val(),
'model' : $('#model' + i).val(),
'reward' : $('#reward' +i).val(),
'subtract' : $('#subtract' + i).val(),
'minimum' : $('#minimum' + i).val(),
'shipping' : $('#shipping' + i).val(),
'tax_class_id' : $('#tax_class_id' + i).val(),
'weight' : $('#weight' + i).val(),
'quantity' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').val(),
'price' : $('table.order-table tbody tr[id=\''+ i +'\']').find('td.price').html(),
'total' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').parent().parent().find('td.total').html()
});
}
希望它會有所幫助。
product[0]['name'] = "apple mac power"
要么
product[0].name = "apple mac power"
代替
product[0][name] = "apple mac power"
你的第一種方式是好的,但你添加一個帶有哈希的數組只刪除周圍的[]
product[i-1] = {
'product_id' : $('#product_id' + i).val(),
'name' : $('#item' + i).val(),
'model' : $('#model' + i).val(),
'reward' : $('#reward' +i).val(),
'subtract' : $('#subtract' + i).val(),
'minimum' : $('#minimum' + i).val(),
'shipping' : $('#shipping' + i).val(),
'tax_class_id' : $('#tax_class_id' + i).val(),
'weight' : $('#weight' + i).val(),
'quantity' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').val(),
'price' : $('table.order-table tbody tr[id=\''+ i +'\']').find('td.price').html(),
'total' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').parent().parent().find('td.total').html()
};
代替
product[i-1] = [{
'product_id' : $('#product_id' + i).val(),
'name' : $('#item' + i).val(),
'model' : $('#model' + i).val(),
'reward' : $('#reward' +i).val(),
'subtract' : $('#subtract' + i).val(),
'minimum' : $('#minimum' + i).val(),
'shipping' : $('#shipping' + i).val(),
'tax_class_id' : $('#tax_class_id' + i).val(),
'weight' : $('#weight' + i).val(),
'quantity' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').val(),
'price' : $('table.order-table tbody tr[id=\''+ i +'\']').find('td.price').html(),
'total' : $('table.order-table tbody tr td.quantity input[name=\'quantity'+ i +'\']').parent().parent().find('td.total').html()
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.