具有输入标记的多行表

时间:2013-06-23 12:32:05

标签: php javascript jquery asp.net web-applications

在发票表单中我有一个表格,其中有25行

<table border="1"  align="center">
<thead>
<tr>
<th>#</th>
<th>Item Name</th>
<th>Item Code</th>
<th>Quantity</th>
<th>Price</th>
<th>Amount</th>-->
<th>Net Amount</th>
</tr>
</thead>

 <tbody>
 <?php for($i=1 ; $i<=25 ; $i++) { ?>

 <tr>
 <th> <?php echo "$i"; ?> </th>
 <td><input id="itemName"  onBlur="loadAllField()" class="orderInput" type="text" align="center" ></td>
 <td><input id="itemCode" onBlur="loadAllField()"  class="orderInput" type="text" align="center" ></td>
<td><input id="quantity" class="orderInput" onKeyPress="calqtyprice()" type="text"         align="center" ></td>
<td><input id="price" class="orderInput" type="text" align="center"  readonly></td>
<td><input id="netAmount" class="orderInput" type="text" align="center" readonly ></td>
</tr>

<?php } ?>
</tbody>

<tfoot>
<tr>
<td id="tdTotal" colspan="2" style="font-size:16px"> <b>Totals</b></td>
<td id="totalspace" colspan="3"> </td>
<td><input id="netTotalAll" class="orderInput" type="text" readonly> </td>
</tr>


</tfoot>
</table>

<p id="msg" align="center"> </p>

和java脚本函数是

 function loadAllField ()
{
    var itemName = document.getElementById("itemName");
var itemCode = document.getElementById("itemCode");
var quantity = document.getElementById("quantity");
var price = document.getElementById("price");
var netAmount = document.getElementById("netAmount");
var msg = document.getElementById("msg");

    msg.innerHTML = "";
// check is fill 
if ( ( (itemName == null ) || (itemName.value == "") ) && ((itemCode == null ) || (itemCode.value == "")) )
{
msg.innerHTML = "";
    itemName.value = null;
    itemCode.value =  null;
    quantity.value = null;
    price.value = null;
    netAmount.value = null;
}

// load id if item name give and load name if item code given

if ((itemName.value == "detol") || (itemCode.value=="1") ) 
{
        var itemName = document.getElementById("itemName");
var itemCode = document.getElementById("itemCode");
var quantity = document.getElementById("quantity");
var price = document.getElementById("price");
var netAmount = document.getElementById("netAmount");
var msg = document.getElementById("msg");
    msg.innerHTML = "";
    itemName.value = "detol";
    itemCode.value = "1";
    quantity.value = "";
    price.value = "40";
    netAmount.value = "";

}

else if  ( (itemName.value == "robin") || (itemCode.value == "2") )  
    {
        msg.innerHTML = "";
    itemName.value = "robin";
    itemCode.value = "2";
    quantity.value = "";
    price.value = "90";
    netAmount.value = "";
    }

    else
    {   
    msg.innerHTML = "Product not find";
    //msg.innerHTML.blink();
    itemName.value = "";
    itemCode.value = "";
    quantity.value = "";
    price.value = "";
    netAmount.value = "";

    }

} // end of load id function

function calqtyprice()
{
    for ( i=1; i==25; i++)
    {

    var price = document.getElementById("price");
    var quantity = document.getElementById("quantity");
    var netAmount = document.getElementById("netAmount");
    var netTotalAll = document.getElementById("netTotalAll");

    var netamt = price.value * quantity.value ;

     netAmount.value = netamt;

    netTotalAll = netAmount.value + netAmount.value;
    }
}

问题是如何遍历每一行并从输入中获取值并计算并在最后一行显示nettotal。

或者这个或教程是否有任何机制。

1 个答案:

答案 0 :(得分:2)

让我们假设您创建了具有唯一ids的元素,如下所示

<input type="text" id="price_1"

在您的情况下,您可以使用$i值创建名为ids的名称。

<input type="text" id="price_<?php echo $i; ?>" 

然后在你的javascript循环中

for ( i=1; i<=25; i++) {
   var price = document.getElementById("price_" + i);

可让您访问每个唯一元素。