Jquery对象数组被覆盖onload [英] Jquery object array gets overwritten onload

查看:83
本文介绍了Jquery对象数组被覆盖onload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档就绪中为我的对象数组分配了值 - > div load被最后一个值覆盖。数组中的每个对象都是相同的(最后一个)。



我尝试了什么:



我正在加载这个对象数组:

< pre> $(document).ready(function(){
$( < div>< / div>)。load(/ Stats / Contents #stats,function(){
statcount = $(。list-group-item,this).length;

for(var j = 0; j< statcount; j ++){
statList.push(stat);
}

for(var i = 0; i< statcount; i ++){
statList [i] .statId = document.getElementById(statId-+(i + 1)+)。value;
statList [ i] .productDescription = document.getElementById(productType-+(i + 1)+)。value;
statList [i] .lastMeasuredInventoryAmount = document.getElementById(statLastMeasureAmount-+(i + 1) )+)。value;
}
)}
)}



..等等。然后我得到更改的值并保存它们,但是,在ajax post调用中,所有数组对象都是相同的(最后一个已分配),看起来它们被覆盖了。有任何想法吗?我看到了这些延迟/承诺类型的代码,但不确定是否有更简单的方法。谢谢。

解决方案

(document).ready(function(){


(< div>< / div> ;)。load(/ Stats / Contents #stats,function(){
statcount =


(。list-group-item,this).length ;

for(var j = 0; j< statcount; j ++){
statList.push(stat);
}

for( var i = 0; i< statcount; i ++){
statList [i] .statId = document.getElementById(statId-+(i + 1)+)。value;
statList [i] .productDescription = document.getElementById(productType-+(i + 1)+)。value;
statList [i] .lastMeasuredInventoryAmount = document.getElementById(statLastMeasureAmount-+(i + 1)+)。value;
}
)}
)}



..等等。然后我得到更改的值并保存它们,但是,在ajax post调用中,所有数组对象都是相同的(最后一个已分配),看起来它们被覆盖了。有任何想法吗?我看到了这些延迟/承诺类型的代码,但不确定是否有更简单的方法。感谢。


My array of objects being assigned values in document ready -> div load gets overwritten with the last value. Each object in the array is same (last one).

What I have tried:

I have this array of objects being loaded:

<pre>$(document).ready(function () {
  $("<div></div>").load("/Stats/Contents #stats", function () {
    statcount = $(".list-group-item", this).length;

    for (var j = 0; j < statcount; j++) {
      statList.push(stat);
    }

    for (var i = 0; i < statcount; i++) {
      statList[i].statId = document.getElementById("statId-" + (i + 1) + "").value;
      statList[i].productDescription = document.getElementById("productType-" + (i + 1) + "").value;
      statList[i].lastMeasuredInventoryAmount = document.getElementById("statLastMeasureAmount-" + (i + 1) + "").value;
    }
  )}
)}


..and so on. Then I get the changed values and save them, however, in the ajax post call, all of the array objects are same (the last one assigned), looks like they get overwritten. Any ideas? I saw these deferred/promise type code but not sure if there's simpler way. Thanks.

解决方案

(document).ready(function () {


("<div></div>").load("/Stats/Contents #stats", function () { statcount =


(".list-group-item", this).length; for (var j = 0; j < statcount; j++) { statList.push(stat); } for (var i = 0; i < statcount; i++) { statList[i].statId = document.getElementById("statId-" + (i + 1) + "").value; statList[i].productDescription = document.getElementById("productType-" + (i + 1) + "").value; statList[i].lastMeasuredInventoryAmount = document.getElementById("statLastMeasureAmount-" + (i + 1) + "").value; } )} )}


..and so on. Then I get the changed values and save them, however, in the ajax post call, all of the array objects are same (the last one assigned), looks like they get overwritten. Any ideas? I saw these deferred/promise type code but not sure if there's simpler way. Thanks.


这篇关于Jquery对象数组被覆盖onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆