2017-07-07 51 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