2016-11-28 126 views
0

我已经使用jQuery创建了一个表行,列ID是已选择的数据的ID。我如何计算ID是动态(基于数据ID)的'total_price'列的总和?如何使用jquery计算动态列的总和

enter image description here

这里我jQuery脚本

function getCorporateService(id){ 
    //get data and parsing to column 
    $.get("{{ url('salesorder/service')}}/"+id, function(data){ 
     console.log(id); 
     console.log(data); 
     $.each(data, function (index, element){ 

      $br = "<tr id='item'>"; 
      $br += "<td> <input class='input-small' type='text' id='order_identifier' name='order_identifier' readonly></td>"; 
      $br += "<td><input class='input-small' type='text' id='service["+id+"]' name='service["+id+"]' value='"+element.service_name+"' readonly></td>"; 
      $br += "<td><select id='order_type["+id+"]' name='order_type["+id+"]'> <option> - </option> <option value='add'>Add</option> <option value='change'>Change</option> <option value='cancel'>Cancel</option> </select></td>"; 
      $br += "<td><input class='input-small' type='text' id='select_plan["+id+"]' name='select_plan["+id+"]'></td>"; 
      $br += "<td><input class='input-mini' type='text' id='qty["+id+"]' name='qty["+id+"]' value='1' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><input class='input-small' type='text' id='unit_price["+id+"]' name='unit_price["+id+"]' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><input class='input-small' type='text' id='total_price["+id+"]' name='total_price["+id+"]' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><textarea class='input-small' id='notes["+id+"]' name='notes["+id+"]'></textarea></td>"; 
      $br += "</tr>"; 

      $(".corporatesvc").append($br); 
     }); 
    }); 
} 
+0

当计算呢?并显示它在哪里? –

+0

“onChange”内联器有什么问题? – Jai

+0

定义var totalPrice = 0;之前,然后totalPrice + = getTotalPrice(“+ id +”); – Kamal

回答

3

要回答你的问题不添加类的字段,使用starts with selector的getTotalPrice内

function getTotalPrice() { 
    var total = 0; 
    $("[id^='total_price']").each(function() { // or $(".total_price") if given a class 
    var val = $(this).val(); 
    total += isNaN(val) || $.trim(val)=="" ? 0 : parseFloat(val); // or parseInt(val,10); 
    }); 
    $("#total").text(total.toFixed(2)); // or (total) if an int 
} 

你应该给每个可更改的输入一个类名并分配一个处理程序 - onkeyup例如和使用委托:

$(function() { 
    $(".corporatesvc").on("keyup",".changeable",function() { 
    if (this.id.indexOf("qty")==0) { 
     // here you can update siblings 
    } 
    getTotalPrice(); 
    }); 
}); 
+0

'$(“[id^='total_price']”)'和'$(“[id ='total_price']”)'都是一样的,对不对? – Dipak

+1

编号'^ ='开头。这将处理'id =“total_price”''''id =“total_price_1”'和'id =“total_price_2”' – mplungjan

+0

嗨:)感谢您的帮助,现在正在工作 – rafitio

1

给每个文本框的类名称。 Check thisLinki hope it helps you. HTML

<input class='input-small total_price' type='text' id='total_price_1' name='total_price_1'> 
<input class='input-small total_price' type='text' id='total_price_2' name='total_price_2' > 
<input class='input-small total_price' type='text' id='total_price_3' name='total_price_3' > 
<p id="classname"> 

JS

$(".total_price").each(function() { 
    $(this).keyup(function() { 
    var sum = 0; 
    $(".total_price").each(function() { 
     if (!isNaN(this.value) && this.value.length != 0) { 
     sum += parseFloat(this.value); 
     } 
     $("#classname").html(sum); 
    }); 
    }); 
}); 
+0

total_price上不会有关键事件领域。您的意思是在字段中添加一个类,然后您可以汇总仅位于total_price字段上的.total_price类 – mplungjan