我正在构建一个装运表单并编写了一个php页面来查询销售订单的mysql数据库并显示该订单中的每个订单项。我希望能够做的是在每个订单项中添加一个包装箱号码(包含产品的物理箱号码)。例如,第1行是针对15个单位的产品A.如果一个盒子装有两个单位的产品A而我运送的是8个零件或四个盒子,我希望能够添加一行,请输入箱号和继续为第2项行做类似的事情。
这是我到目前为止所做的:
<script language="javascript">
// Last updated 2006-02-21
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow + 1;
var row = tbl.insertRow(lastRow);
// Item cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
// Cust PN cell
var cellBox_Num = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'Box_Num[]';
el.id = 'Box_Num' + iteration;
el.size = 18;
el.onkeypress = keyPressTest;
cellBox_Num.appendChild(el);
}
function keyPressTest(e, obj)
{
var validateChkb = document.getElementById('chkValidateOnKeyPress');
if (validateChkb.checked) {
var displayObj = document.getElementById('spanOutput');
var key;
if(window.event) {
key = window.event.keyCode;
}
else if(e.which) {
key = e.which;
}
var objId;
if (obj != null) {
objId = obj.id;
} else {
objId = this.id;
}
displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
}
}
function removeRowFromTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
if (lastRow > 1) tbl.deleteRow(lastRow - 1);
}
</script>
//define variables and some other form stuff
$i=0;
$n=1;
while($i < $num1) {
$SO_Line_Item=mysql_result($result1,$i,"SO_Line_Item");
$My_PN=mysql_result($result1,$i,"My_PN");
$My_PN_Rev=mysql_result($result1,$i,"My_PN_Rev");
$Cust_PN=mysql_result($result1,$i,"Cust_PN");
$Cust_PN_Rev=mysql_result($result1,$i,"Cust_PN_Rev");
$Description=mysql_result($result1,$i,"Description");
$SOItemQty=mysql_result($result1,$i,"Qty");
$UOM=mysql_result($result1,$i,"UOM");
$Program=mysql_result($result1,$i,"Program");
$Required_Date=mysql_result($result1,$i,"Required_Date");
$SOItemShipQty=mysql_result($result2,$i,"count(My_SN)");
?>
<tr>
<td width="3px" valign="top" align="center"><?php
echo "$SO_Line_Item";?>
</td>
<td nowrap="nowrap" valign="top" align="center"><?php
echo "$Cust_PN - $Cust_PN_Rev";?>
</td>
<td nowrap="nowrap" valign="top" align="center"><?php
echo "$My_PN - $My_PN_Rev";?>
</td>
<td nowrap="nowrap" valign="top" align="center"><?php
echo "$Description";?>
</td>
<td nowrap="nowrap" valign="top" align="center">
<?php
echo "$SOItemQty";?>
</td>
<td nowrap="nowrap" valign="top" align="center">
<?php
echo "$UOM";?>
</td>
<td nowrap="nowrap" valign="top" align="center">
<?php
echo $SOItemQty - $SOItemShipQty;?>
</td>
<td nowrap="nowrap" valign="top" align="center">
<input size="2" align="right" type="text" name="Qty">
</td>
<td nowrap="nowrap" valign="top" align="center">
<?php
echo "$Required_Date";?>
</td>
<td nowrap="nowrap" valign="top" align="center">
<?php
echo "$Program";?>
</td>
</tr>
<tr>
<td colspan="10">
<p>
<input type="button" value="Add" onclick="addRowToTable();" />
<input type="button" value="Remove" onclick="removeRowFromTable();" />
<!--
<input type="button" value="Submit" onclick="validateRow(this.form);" />
<input type="checkbox" id="chkValidate" /> Validate Submit
-->
<b><font color="RED">Add/Remove Box Number For This Line Item</font></b>
</p>
</td>
</tr>
<tr>
<td></td>
<td>
<table id="tblSample">
<tr>
<td>1</td>
<td><input type="text" name="Box_Num[]"
id="Box_Num1" size="18" onkeypress="keyPressTest(event, this);" /></td>
</tr>
</table>
</td>
</tr>
<?php
$i++;
}
我能够很好地创建表单,但我的添加和删除按钮都会影响循环的第一个实例。这是可以理解的,因为表ID是循环内所有实例的'tblSample'。我迷失了如何为每个循环实例创建动态Javascript。