2017-07-07 50 views
2

我想創建一個餐廳訂單表單,其中我在文本框中輸入值,並將使用Ajax按鈕點擊後的小計和總計顯示在列表中。在刪除動態列的值的總和錯誤計算

我的問題是,小計的總和給了我錯誤的值 ,並且如果我點擊刪除按鈕則不會改變。

| qty |價格|小計|
| 10 | 3.50 | 35.00 |
| 10 | 9.00 | 90.00 |

總計:125.00的180.00代替

其中它計算行×最後小計值的數量。

請參閱TIA附帶的代碼片段!

var impo = document.getElementById("imp_text"); 
 
var quant = document.getElementById("qta"); 
 

 
$(document).on("click", "table.dynatable button.delete-row", function() { 
 
    $(this).parents("tr").remove(); 
 
}); 
 

 
function loaddata() { 
 
    var importo = $("#imp_text").val(); 
 
    var quantita = $("#qta").val(); 
 
    var totale = importo * quantita; 
 
    var desc_importo = "Altro "; 
 
    var markup = "<tr><td><span class='sum_qta' name='sum_qta'>" + quantita + "</span></td><td>" + desc_importo + "</td><td>" + importo + "</td><td class='subtot' >" + totale + "</td><td><button type='button' class='delete-row'>X</button></td></tr>"; 
 

 
    $("table.dynatable tbody").append(markup); 
 

 
    var $tblrows = $("#tableordine tbody tr"); 
 
    $tblrows.each(function(index) { 
 
    var $tblrow = $(this); 
 

 
    if (!isNaN(totale)) { 
 

 
     $tblrow.find('.subtot').val(totale.toFixed(2)); 
 
     var grandTotal = 0; 
 

 
     $(".subtot").each(function() { 
 
     var stval = parseFloat($(this).val()); 
 
     grandTotal += isNaN(stval) ? 0 : stval; 
 
     }); 
 

 
     $('.grdtot').val(grandTotal.toFixed(2)); 
 
    } 
 

 
    }); 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<html lang="en"> 
 

 
<head> 
 
</head> 
 

 
<body> 
 
    <font color="black"> 
 

 
    <table class="table dynatable" id="tableordine"> 
 
     <thead> 
 
     <tr> 
 
      <th>Qty</th> 
 
      <th>Import</th> 
 
      <th>Price</th> 
 
      <th>subtotal</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 

 
     </tbody> 
 
    </table> 
 

 
    <hr/> Totale:<input type="text" class="grdtot"> 
 

 
<hr/> 
 
    <FORM name="Keypad" action=""> 
 
     Import : <input name="ReadOut" id="imp_text" type="Text" size=24 value=" "> Quantity : <input type="text" name="readqta" id="qta" value="1" /> 
 
     <p> 
 

 
     <input type="button" id="entraordine" class="add-row menu_button" value="Entra" onclick="loaddata();" /> 
 
    </form> 
 
</body> 
 
</html>

+1

去,我建議你先寫你自己的按鈕單擊事件處理程序,而不是使用'的onclick = 「」'HTML屬性。應該是這樣的: ''(「body」)。on(「click」,「#entraordine」,function(){ //代碼 });' –

+0

也爲什麼你有'val(totale。 toFixed(2))''而不是''tblrow.find('。subtot')。中的'.val()'.val(totale.toFixed(2));'?? –

+0

@ N.Ivanov,以便我可以將類.subtot的值作爲totale和tofxed(2)指定爲小數 – OFBrc

回答

1

在這裏,您與解決方案https://jsfiddle.net/tbskqrby/

$(document).on("click", "table.dynatable button.delete-row", function() { 
    $(this).parents("tr").remove(); 
    rowCal(); 
}); 

rowCal = function(){ 
    var $tblrows = $("#tableordine tbody tr"); 
    $tblrows.each(function(index) { 
     var $tblrow = $(this); 

     if (!isNaN(totale)) { 

      $tblrow.find('.subtot').val(totale.toFixed(2)); 
      var grandTotal = 0; 

      $(".subtot").each(function() { 
       var stval = parseFloat($(this).val()); 
       grandTotal += isNaN(stval) ? 0 : stval; 
      }); 

      $('.grdtot').val(grandTotal.toFixed(2)); 
     } 

    }); 
} 
+0

現在單擊刪除按鈕時總值會發生變化,但會給出合計的錯誤值 – OFBrc

+0

@Shiladitya你能解釋爲什麼這是解決方案嗎? – phuzi

+1

@OFBrc在這裏你可以使用解決方案https://jsfiddle.net/tbskqrby/1/。問題在於你的var stval = parseFloat($(this).val()); 。我將其更改爲var stval = parseFloat($(this).text());獲得價值。 – Shiladitya