简体   繁体   English

JavaScript的总价和数量

[英]Total price and Quantity in Javascripts

Hi guys am trying to get the Total price and quantity of each items. 大家好,我想获取每个项目的总价格和数量。 So, i have a fixed price of $20. 因此,我的固定价格为20美元。 the price comes out fine but its not counting the quantity of each items.... 价格还算可以,但是没有计算每件商品的数量。

HTML code HTML代码

HTML-Continous HTML连续

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. 价格= 20数量= 1 价格= 40数量= 2 ''''因为我有20的固定价格。 Any help will be appreciated. 任何帮助将不胜感激。

The reason is you are using same function name for all onchange function. 原因是您对所有onchange函数使用相同的函数名。 ie, for updating price and total you are using the same changeval1() function. 即,为了更新价格和总计,您使用相同的changeval1()函数。

Try changing name of the function. 尝试更改函数名称。 Both function has same name. 这两个函数具有相同的名称。 One function updates total and another updates price . 一个功能更新总计 ,另一个功能更新价格 So when both function has same name, both price and total gets updated. 因此,当两个函数具有相同的名称时,价格和总计都会更新。

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). 如果要同时获得TotalQuantity和TotalPrice,则应首先计算TotalQuantity,然后计算TotalPrice = TotalQuantity * 20(因为所有商品的价格都固定)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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