douruye5092 2018-11-05 11:23 采纳率: 100%
浏览 93

如何自动计算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>
  • 写回答

1条回答 默认 最新

  • douxiajia6104 2018-11-05 12:03
    关注

    please read How to ask a good question before asking some question.

    it could be done using jquery. learn 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>

    keep coding! keep hustling!

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?