php - 具有输入标签的多行表

标签 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。

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

最佳答案

假设您创建了具有唯一 id 的元素,如下所示

<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);

让您可以访问每个独特的元素。

关于php - 具有输入标签的多行表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260771/

相关文章:

php - 为什么 PHP 拒绝在 Windows 上启用 cURL

javascript - 如何在运行时使用属性名称动态获取对象属性

php - Heroku 上传和脚本执行限制

php - Steam 身份验证 session

c# - 在 C# 和 ASP.NET 的 javascript 警报中使用 %s

javascript - 如何对javascript中的字符串进行数字排序

javascript - 使用 jquery 或 javaScript 更改背景位置的值

javascript - Bootstrap Slides 在使用 Angular 6 使用多项目轮播时滞后

php - INET_ATON 在 where 语句 doctrine2 querybuilder zend2

javascript - 类数组对象转换为数组