javascript中的数组出错
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()
}];
}
所以在这样做之后,它只显示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是您的数组,数组中的每个单元格都是一个对象,其属性包括名称。
在您的第一个版本中,您将一个数组分配给数组,因此,它显示为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()
}];
相关文章:
- javascript中的数组出错
- 从函数中的函数返回数组时出错
- 角度 2 - 推送到要用 *ngFor 显示的数组时出错
- 完成ajax调用数组的循环,而不是在某个调用出错时中断
- 将jquery ajax请求推入数组时出错
- 比较javascript中的数组时出错
- 在for循环中设置断点会导致在数组上使用原型时出错
- 从AngularJS中的数组中删除元素时出错
- 尝试将数组从内容脚本发送到弹出脚本时出现 Chrome 扩展程序时出错
- 在 JavaScript 中遍历大型数组而不超时时出错
- 使用 Mongoose 和 Node 将数据插入数组时出错
- 使用 Lodash 合并数组,为什么结果是重复的数组值?,我的代码在哪里出错
- 在javascript中创建多维数组,自定义函数中出错
- 从数组查询获取对象 JSON 值时出错
- 在数组中选择对象时出错
- Jade从路由器传递对象/数组时出错
- 使用Mongoose更新数组中的特定对象时出错
- 如何避免在访问JavaScript数组中的元素时出错
- '应为表达式'初始化多维数组时出错
- Highchart没有't显示图形:在序列中添加javascript数组时出错