简体   繁体   中英

How calculate values automatically javaScript

I am developing a system to register purchase slips, the user enters the purchased products and the system directs it to the following web page, I do not know the number of elements of the array products and it will depend on the products selected by the user. How can I automatically calculate the total and total prices when the user enters the quantities and unit prices using javaScript or jQuery?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">


    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <table class="table table-striped">
                <thead>
                    <tr>
                        <th scope="col">Product code</th>
                        <th scope="col">Name</th>
                        <th scope="col">Quantity</th>
                        <th scope="col">Unit price</th>
                        <th scope="col">Total price</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($products as $product):?>
                    <tr>
                        <th scope="row"><?php echo $product["PRODUCTCODE"];?></th>
                        <td><?php echo $product["NAME"];?></td>
                        <td><input type="text" class="form-control" value=""></td>
                        <td><input type="text" class="form-control" value=""></td>
                        <td></td>
                    </tr>
                    <?php
                    endforeach;
                    ?>

                </tbody>
            </table>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <h2>Total:</h2>
                </div>
            </div>



        </div>

        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    </div>
</body>
</html>

 $('#quantity').change( function() { var first_price = $('#product-price').text(); var quant = $('#quantity').val(); var price = quant * first_price; $('#product-line-price').html(price); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="product-price">12.99</div> <input id="quantity" type="number" value="2" min="1"> <h3>total</h3> <div id="product-line-price">25.98</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM