calulate function in javascript for dynamically added field

前端 未结 2 2063
情话喂你
情话喂你 2020-12-22 11:40

I want to calculate subtotal and total for my form

i tried some javascripts but dident worked for me

some of them were only for static table since i am addi

相关标签:
2条回答
  • 2020-12-22 12:04

    A little something I put together after reading your issue. This still needs plenty of work doing to it but this should get you on the right lines.

    I have added comments within the source code to explain step by step.

    Add more will duplicate the contents of the table but this is just for demo use to show you can still trigger events with dynamic elements.

    Discount will be deducted as a percent of the price.

    $(document).on('input','.Calc',function(){
    // Reset Total
    var Total=0;
    $('.Inputs').each(function(){
    // Reset Price.
    var Price=0;
    // Reset Decution.
    var Deduction=0;
    //  Price x Quantity
    Price=parseFloat($(this).find(".Qty").val())*parseFloat($(this).find(".Price").val());
    var Percent=parseFloat($(this).find(".Disc").val());
      // If Discount is greater than 0 make deductions.
      if(Percent>0){
      // Calculate the discount Percent of the Price.
      // Example 300*40/100 = 120
      Deduction=Price*Percent/100;
      // Minus discount from the price.
      Price=Price-Deduction;
      }
    // Sub total, add Price to existing Total
    Total=Total+Price;
    // Display Price and savings "£0 (Save £0)"
    $(this).find("span").html('&pound;'+Price+' <small>(Save &pound;'+Deduction+')</small>');
    });
    // Display New Total.
    $('#Total').html('Sub Total &pound;'+Total);
    });
    
    //--------- Clone / Append (Demo Use)
    $('#More').on('click',function(){
    // This is used to show you can run the functions even if the elements are dynamic. 
    // (This is very messy but it shows that it works)
    var target = document.getElementById('MyForm');
    var tr = document.createElement('tr'); 
    tr.setAttribute('class', 'Inputs'); // Edit this 
    target.appendChild(tr);
    var td1 = document.createElement('td');  
    tr.appendChild(td1);
    td1.innerHTML='Qty';
    var td2 = document.createElement('td');  
    tr.appendChild(td2);
    td2.innerHTML='&pound;';
    var td3= document.createElement('td');  
    tr.appendChild(td3);
    // Add inputs. 
    td3.innerHTML='Discount:';
    var QTY = document.createElement('input'); 
    var PRICE = document.createElement('input'); 
    var DISC = document.createElement('input'); 
    QTY.setAttribute('type','text');
    QTY.setAttribute('class','Qty Calc');
    QTY.setAttribute('value',1);
    PRICE.setAttribute('type','text');
    PRICE.setAttribute('class','Price Calc');
    PRICE.setAttribute('value',0);
    DISC.setAttribute('type','text');
    DISC.setAttribute('class','Disc Calc');
    DISC.setAttribute('value',0);
    td1.appendChild(QTY);
    td2.appendChild(PRICE);
    td3.appendChild(DISC);
    td3.innerHTML=td3.innerHTML+'% <span></span>';
    });
    /* CSS for Demo Use. */
    .Qty{width:30px;}
    .Price{width:70px;}
    .Disc{width:30px;}
    span{color:red;font-weight:bold;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <button id="More">Add</button>
    <table id="MyForm">
    <tr class="Inputs">
      <td>Qty<input type="text" class="Qty Calc" value="1"/></td>
      <td> &pound;<input type="text" class="Price Calc" value="0"/></td>
    <td> Discount:<input type="text" class="Disc Calc" value="0"/>% <span></span></td>
    </tr>
    </table>
    <span id="Total"></span>

    If you have any questions about the source code above please leave a comment below and I will reply as soon as possible.

    I hope this helps. Happy coding!

    This is using jQuery

    Update: Replaced clone() with createElement()

    0 讨论(0)
  • 2020-12-22 12:06

    If I understood you correctly, the code you are looking for should look something like this:

    var values = document.querySelectorAll('input.amnt'),//find all inputs  
                                                    //that you need to add 
                                                    //use regular dom selector 
                                                    //like in css
        subtotal = 0;
    
    for(var i = 0, ; = values.length; i<l; i++ ) {
        subtotal += values[i].value;
    }
    

    It'll always add all the vales in the documents, you can add or remove data, doesn't matter.

    PS please do not use <center> tag - it's deprecated, you should be using css for that.

    0 讨论(0)
提交回复
热议问题