输入价格后,如何在表格中添加另一行并计算金额(数量*价格)?

时间:2019-05-09 10:04:05

标签: javascript php html

我在一个页面上有一个表格,我们可以在其中选择产品并输入数量和价格,然后应在最后一栏中计算数量,并添加新行以输入更多产品,直到完成为止。

<table id="products_table" class="table table-hover" name="products">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Quantity</th>
            <th scope="col">Pieces</th>
            <th scope="col">Price</th>
            <th scope="col">Amount</th>                                 
        </tr>
    </thead>
    <tbody> 
        <tr class="" id="templateRow">                  
            <td>
                <select style = "margin-left:20px" name="product_id">
                <?php
                    while($r = mysqli_fetch_assoc($presult))
                    {
                        ?>
                        <option value =<?php echo $r["id"]?>><?php echo $r["name"]?></option>
                        <?php
                    }
                ?>
                </select>
            </td>
            <td><input type="float" name="quantity" oninput="SetQuantity(this)"></td>
            <td><input type="float" name="pieces"></td>
            <td><input type="float" name="price" oninput="calculate(this)"></td>
            <td><input type="text"></td>
        </tr>
    </tbody>
</table>

<script>

var quantity=0;

function SetQuantity(row)
{
    quantity = row.value;
}

function addrow() 
{
    var table = document.getElementsByTagName('table')[0];
    var length = table.rows.length; 
    table.insertRow(table.rows.length);
    alert(length);  
}

function calculate(row)
{
    alert(row.value * quantity);
    addrow();
}
</script>

1 个答案:

答案 0 :(得分:1)

尝试这样。

var quantity=0;
var total = 0;
var price = 0;

function addrow() 
{
    var table = document.getElementsByTagName('table')[0];
    var length = table.rows.length; 
    table.insertRow(table.rows.length);
    alert(length);  
}

function calculate(qtyId,priceId,id)
{
     quantity = document.getElementById(qtyId).value;
     price = document.getElementById(priceId).value;

     total = quantity * price;
     if(isNaN(total))
      total = 0;
      
    document.getElementById(id).value = total;
}


var index = 1;
function insertRow(){
            var table=document.getElementById("products_table");
            var row=table.insertRow(table.rows.length);
            var cell0=row.insertCell(0);
            cell0.innerHTML="Name";
            var cell1=row.insertCell(1);
            var t1=document.createElement("input");
            t1.addEventListener('input',function(){ calculate('quantityId'+(index-1),'priceId'+(index-1), 'amountId'+(index-1))})
                t1.id = "quantityId"+index;
                cell1.appendChild(t1);
            var cell2=row.insertCell(2);
            var t2=document.createElement("input");
                t2.id = "piecesId"+index;
                cell2.appendChild(t2);
            var cell3=row.insertCell(3);
            var t3=document.createElement("input");
                t3.id = "priceId"+index;
                t3.addEventListener('input',function(){ calculate('quantityId'+(index-1),'priceId'+(index-1), 'amountId'+(index-1))})
                cell3.appendChild(t3);
            var cell4=row.insertCell(4);
            var t4=document.createElement("input");
                t4.id = "amountId"+index;
                cell4.appendChild(t4);
      index++;

}
<input type="button" onClick="insertRow()"/>
<table id="products_table" class="table table-hover" name="products">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Quantity</th>
            <th scope="col">Pieces</th>
            <th scope="col">Price</th>
            <th scope="col">Amount</th>                                 
        </tr>
    </thead>
    <tbody> 
        <tr class="" id="templateRow">                  
            <td>
                name
            </td>
            <td><input type="float" name="quantity" id="quantityId" oninput="calculate('quantityId','priceId','amountId')"></td>
            <td><input type="float" name="pieces" id="piecesId"></td>
            <td><input type="float" name="price" id="priceId" oninput="calculate('quantityId','priceId','amountId')"></td>
            <td><input type="text" id="amountId"></td>
        </tr>
    </tbody>
</table>