简体   繁体   English

jQuery计算总数与数量和步骤

[英]jquery calculate total with quantity and steps

I need help! 我需要帮助! I need a jquery script to calculate total: I need 1 input field for quantity. 我需要一个jquery脚本来计算总数:我需要1个输入字段来表示数量。 If the quantity is: between 1 and 60, the price is 750, between 61 and 90, the price is 850, between 91 and 120, the price is 995, I also need to add new row with quantity input field and I need to calculate the total price of all. 如果数量是:1到60之间,价格是750,61到90之间,价格是850,91到120之间,价格是995,我还需要在数量输入字段中添加新行,我需要计算所有的总价。 I have already tried but don't know how to do it. 我已经尝试过,但是不知道该怎么做。

Thanks. 谢谢。
I have tried the following: 我尝试了以下方法:

<script type="text/javascript">
    function miaFunzione() {
        $("#Container").append("<li><input type='text' class='qty' value='' placeholder='superfice' /> <a onclick='miaFunzione()'>aggiungi [+]</a></li>");

        var totale = 0;

        $("#Container li").each(function () {
            var qty = new Number($(this).children('.qty').val());

            if (qty <= 60) {
                prezzo = 750;
            }else if (qty >= 61 && qty <= 90){
                prezzo = 850;
            }else if (qty >= 91 && qty <= 120){
                prezzo = 995;
            };

            totale = totale + prezzo;

        });

        $("#totMoney").html(totale);
    }
</script>

this is the html code 这是HTML代码

<ul id="Container">
    <li>
        <input type="text" value="" class="qty" placeholder="superfice" /><a onclick="miaFunzione()">aggiungi [+]</a>
    </li>
</ul>
<br />

TOTALE COSTO: € <strong id="totMoney">0</strong>

you can use this 你可以用这个

 $(document).ready(function(){ $('body').on('keyup','li > .qty',function(){ var totale = 0; $("#Container li .qty").each(function () { var qty = parseFloat($(this).val()); if (qty <= 60) { prezzo = 750; }else if (qty >= 61 && qty <= 90){ prezzo = 850; }else if (qty >= 91 && qty <= 120){ prezzo = 995; } totale += qty + prezzo; }); $("#totMoney").html(totale); }); $('body').on('click','li > a',function(){ $("#Container").append("<li><input type='text' class='qty' value='' placeholder='superfice' /> <a>aggiungi [+]</a></li>"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="Container"> <li> <input type="text" value="" class="qty" placeholder="superfice" /><a>aggiungi [+]</a> </li> </ul> <br /> TOTALE COSTO: € <strong id="totMoney">0</strong> 

Check this jsfiddle I made for you https://jsfiddle.net/n5zoyxq3/ 检查我为您制作的这个jsfiddle https://jsfiddle.net/n5zoyxq3/

function miaFunzione() {
    $("#Container").append("<li><input type='text' class='qty' value='' placeholder='superfice' /> <a onclick='miaFunzione()'>aggiungi [+]</a></li>");

    var totale = 0;
    var prezzo = 0;

    $("#Container li").each(function () {
        var qty = parseFloat($(this).children('.qty').val());
    if(qty > 0) {
      if (qty <= 60) {
        prezzo = 750;
      }else if (qty >= 61 && qty <= 90){
        prezzo = 850;
      }else if (qty >= 91 && qty <= 120){
        prezzo = 995;
      };
    }

        totale = totale + prezzo;
        prezzo = 0;

    });

    $("#totMoney").html(totale);
}

I've changed the line "var qty = new Number($(this).children('.qty').val());" 我已经更改了行“ var qty = new Number($(this).children('。qty')。val());” to "var qty = parseFloat($(this).children('.qty').val());" 到“ var qty = parseFloat($(this).children('。qty')。val());” just to avoid error in case '.qty' has no numeric value. 只是为了避免在'.qty'没有数字值的情况下出错。

Below that line I added an "if" to check if var qty has value because your user can add various inputs without value. 在该行下面,我添加了一个“ if”来检查var qty是否有价值,因为您的用户可以添加各种没有价值的输入。

After assigning the prezzo to totale I changed prezzo to "0" so in the next loop prezzo will start with value zero. 在将prezzo分配给总计后,我将prezzo更改为“ 0”,因此在下一个循环中,prezzo将以零值开始。

Hope it helps you. 希望对您有帮助。

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

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