【发布时间】: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