【问题标题】:Building Arrays dynamically for inserting into SQL动态构建数组以插入 SQL
【发布时间】:2015-09-16 18:00:45
【问题描述】:

我有一个表格,其中包含单元格中的输入。这些输入需要在一次插入中插入到 SQL 中。我在尝试构建阵列时遇到了麻烦。我可以将输入到数组中的值推入没问题。当我推送另一个输入时,第一个数组被覆盖。我需要恢复数组或创建一个新数组,以便存储第一个数组的内容,并将第二个数据数组的值全部存储在一个数组中。

我希望用户单击一个保存按钮,该按钮获取所有数据并将其插入 sql。可能只有 1 个数据数组或 3 个数组或 10 个数组。所以我想问题变成了;如何创建一个数组并在全局数组中推送值,然后创建另一个数组并在全局数组中推送值而不清除第一个数组的值?

我还没有收到 AJAX 请求,只是在准备构建数组。

非常感谢任何帮助。

HTML

<tr class="rows" id="row3" >
   <td class="celltimes4a"id="row3Project"></td>
   <td class="celltimes4c"id="row3Name">General</td>
   <td class="celltimes4"id="row3Sun" ><input id="num3Sun" class="alignRight"  type="text" name="hours" value="" onchange="tott(this)"></input></td>
   <td class="celltimes4"id="row3Mon" ><input id="num3Mon" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
   <td class="celltimes4"id="row3Tue" ><input id="num3Tue" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
   <td class="celltimes4"id="row3Wed" ><input id="num3Wed" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
   <td class="celltimes4"id="row3Thu" ><input id="num3Thu" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
   <td class="celltimes4"id="row3Fri" ><input id="num3Fri" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
   <td class="celltimes4"id="row3Sat" ><input id="num3Sat" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
   <td class="celltimes4b"id="total3"></td>
</tr>

JavaScript

  var temp = {};
  var SqlArr = [];
      function tott(element) {


     var totwLeg = element.id;
     var splitNumero = totwLeg.split(/([A-Za-z]+)([0-9]+)/);
     var getNumero = splitNumero[2];
     var getDay = splitNumero[3];
     var EmpId = document.getElementById('num1').value;
     var WeekEnding = document.getElementById('theDate').value;
     var DateOccur = document.getElementById('row1' + getDay).innerHTML;
     var JobNum = getNumero - 2;
     var Customer = getNumero - 2;
     var HourValue = document.getElementById('num' + getNumero + getDay).value;
     var cnt = 0;
     Empdata = 'EmpData' + cnt + '';
     temp = {
         EmpId, WeekEnding, DateOccur, JobNum, Customer, HourValue
     };
     SqlArr.push({
         Empdata: temp
     });


 }

结果

temp=EmpId="2", WeekEnding="09-19-2015",DateOccur="09-14-2015",JobNum=6,Customer=6,HourValue="2"

期望的结果

temp={EmpId="2", WeekEnding="09-19-2015",DateOccur="09-14-2015",JobNum=6,Customer=6,HourValue="2"},{EmpId="2", WeekEnding="09-19-2015",DateOccur="09-16-2015",JobNum=6,Customer=6,HourValue="4"},{EmpId="2", WeekEnding="09-19-2015",DateOccur="09-16-2015",JobNum=6,Customer=6,HourValue="5"}

【问题讨论】:

    标签: javascript jquery arrays performance multidimensional-array


    【解决方案1】:

    不确定这一切都是错的,但首先将var SqlArr = []; 从您的function tott() 中取出。每次调用该函数时,都会创建一个新的空数组。因此,当您致电 SqlArr.push() 时,您总是会推送数组中的第一项。

    好的,它看起来对我有用。看看这个小提琴:http://jsfiddle.net/r1L9oj80/1/

    但是,您可能会查看您的“DateOccur”变量。它包含您的整个输入元素。

    【讨论】:

    • 是的,我明白了...这是出于测试目的...让我解决这个问题...在原始脚本中它是一个全局变量
    【解决方案2】:

    您的temp 对象无效,请这样尝试...

    temp = {
      'EmpId': EmpId, 'WeekEnding': WeekEnding, 'DateOccur': DateOccur, 'JobNum': JobNum, 'Customer': Customer, 'HourValue': HourValue
    };
    

    加上数组SqlArr 应该在另一个答案中提到的函数之外定义......

    【讨论】:

      猜你喜欢
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 2013-11-01
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多