javaScript如何自动计算值

时间:2018-11-05 11:23:02

标签: javascript php jquery html

我正在开发一个用于注册采购单的系统,用户输入购买的产品,然后系统将其定向到以下网页,我不知道阵列产品的元素数量,它取决于所选产品由用户。 当用户使用javaScript或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>

1 个答案:

答案 0 :(得分:0)

在问一些问题之前,请先阅读How to ask a good question

可以使用jquery完成。 学习selector jQuery。

$('#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>

继续编码!保持忙碌!