douzhi7070 2019-03-18 07:30
浏览 157
已采纳

Javascripts中的总价格和数量

Hi guys am trying to get the Total price and quantity of each items. So, i have a fixed price of $20. the price comes out fine but its not counting the quantity of each items....

HTML code

HTML-Continous

This is the code for Total Quantity and this works fine in an input field.

function changeval() {
    $total = parseInt($("#A1").val()) + parseInt($("#B1").val()) + parseInt($("#C1").val()) + parseInt($("#D1").val()) + parseInt($("#E1").val());

    $('.A').val($("#A1").val());
    $('.B').val($("#B1").val());
    $('.C').val($("#C1").val());
    $('.D').val($("#D1").val());
    $('.E').val($("#E1").val());
    $('.total').val($total);
}

HERE IS THE PROBLEM when i add this code, the Price comes out good but the quantity no longer SHOWS.

function changeval() {
    $price = parseInt($("#A1").val() * 20) + parseInt($("#B1").val() * 20) + parseInt($("#C1").val() * 20) + parseInt($("#D1").val()* 20) + parseInt($("#E1").val()* 20);

    $('.A').val($("#A1").val());
    $('.B').val($("#B1").val());
    $('.C').val($("#C1").val());
    $('.D').val($("#D1").val());
    $('.E').val($("#E1").val());
    $('.price').val($price);
}

so i want to have something like this

PRICE = 20 QUANTITY= 1 PRICE = 40 QUANTITY= 2 '' '' since i have 20 as a fixed price. Any help will be appreciated.

  • 写回答

2条回答 默认 最新

  • dongxu7121 2019-03-18 08:57
    关注

    If you want to get both TotalQuantity & TotalPrice, you should calculate the TotalQuantity first, then calculate TotalPrice = TotalQuantity * 20 (since you have fixed price for all items).

    $(document).ready(function() {
      // Handle onchange event for input number
      $('.qty').on('input', function() {
    
        $qty_total = 0;
        // Calculate total qty
        $(".qty").each(function() {
          $qty_total += parseInt($(this).val());
        });
    
        // Update to Qty input
        $("#qty_total").val($qty_total);
    
        // Update to Price input
        $("#price_total").val($qty_total * 20);
      })
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div>
      <table>
        <tr>
          <td>Item A</td>
          <td><input value=1 type="number" id="qty1" class="qty"></td>
        </tr>
        <tr>
          <td>Item B</td>
          <td><input value=2 type="number" id="qty2" class="qty"></td>
        </tr>
        <tr>
          <td>Item C</td>
          <td><input value=3 type="number" id="qty3" class="qty"></td>
        </tr>
        <tr>
          <td>Item D</td>
          <td><input value=4 type="number" id="qty4" class="qty"></td>
        </tr>
        <tr>
          <td>Item E</td>
          <td><input value=5 type="number" id="qty5" class="qty"></td>
        </tr>
        <tr>
          <td>Total Qty</td>
          <td><input id="qty_total"></td>
        </tr>
        <tr>
          <td>Total Price</td>
          <td><input id="price_total"></td>
        </tr>
      </table>
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号