json 对象推送使用循环中的最后一项覆盖所有项目?
json object push overwrite all item using last item in the loop?
var options = [];
$(b).each(function (a) {
debugger;
s += '<option value=\'' + b[a].Material + '\'>' + b[a].Material + '</option>'
item = {};
item["name"] = b[a].Material;
item["value"] = b[a].Material;
item["checked"] = false;
options.push(item);
});
console.log(options);
s
的值为
<option value='MAB380-01'>MAB380-01</option>
<option value='MAB380-02'>MAB380-02</option>
在第一次迭代中,options
有 MAB380-01
。
但是在第二次迭代之后,options
有 2 个 MAB380-02
.
Firefox 和 Chrome 给我正确的结果,但 IE 没有。
var options = [];
$(b).each(function (a) {
debugger;
s += '<option value=\'' + b[a].Material + '\'>' + b[a].Material + '</option>'
var item = new Object();
item.name = b[a].Material;
item.value = b[a].Material;
item.checked = false;
options.push(item);
});
console.log(options);
这些代码适用于 Chrome、Firefox 和 IE11。
var options = [];
$(b).each(function (a) {
debugger;
s += '<option value=\'' + b[a].Material + '\'>' + b[a].Material + '</option>'
item = {};
item["name"] = b[a].Material;
item["value"] = b[a].Material;
item["checked"] = false;
options.push(item);
});
console.log(options);
s
的值为
<option value='MAB380-01'>MAB380-01</option>
<option value='MAB380-02'>MAB380-02</option>
在第一次迭代中,options
有 MAB380-01
。
但是在第二次迭代之后,options
有 2 个 MAB380-02
.
Firefox 和 Chrome 给我正确的结果,但 IE 没有。
var options = [];
$(b).each(function (a) {
debugger;
s += '<option value=\'' + b[a].Material + '\'>' + b[a].Material + '</option>'
var item = new Object();
item.name = b[a].Material;
item.value = b[a].Material;
item.checked = false;
options.push(item);
});
console.log(options);
这些代码适用于 Chrome、Firefox 和 IE11。