【发布时间】:2020-06-17 21:18:35
【问题描述】:
我有一个带有循环的函数,可以将数据保存在对象中, foreach 循环正在保存最后一个值
var n = new Array();
var weight = new Array();
var obj = {};
var as = [];
// var temp;
$("input:checked").each(function(){
temp = this.value;
// n.push(this.value);
obj['ids'] = temp;
obj['weight']= $("#weight"+temp).val();
as.push(obj);
delete temp;
});
console.log(as);
console.log 的结果是
(5) [{…}, {…}, {…}, {…}, {…}]
0: {ids: "298", weight: "1.00"}
1: {ids: "298", weight: "1.00"}
2: {ids: "298", weight: "1.00"}
3: {ids: "298", weight: "1.00"}
4: {ids: "298", weight: "1.00"}
这应该是
(5) [{…}, {…}, {…}, {…}, {…}]
0: {ids: "on", weight: "undefined"}
1: {ids: "4", weight: "1"}
2: {ids: "5", weight: "2"}
3: {ids: "6", weight: "3"}
4: {ids: "298", weight: "4"}
【问题讨论】:
-
你能分享你正在使用的这段代码的 HTML 吗?
标签: javascript jquery html json object