2014-01-22 143 views
0

我有以下代碼:計算總價jQuery中

for(i=0;i<data.length;i++){ 
    // alert(data[i].stock_id); 
    html1 += '<tr>\n\ 
      <td><input type="text" style="width:80px !important;" readonly id="patient_name' + i + '" name="patient_name[]" value="'+data[i].patient_name+'"/></td>\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="description' + i + '" name="description[]" value="'+data[i].description+'"/></td>\n\\n\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="amount' + i + '" name="amount[]" value="'+data[i].amount+'"/></td>\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="qauntity' + i + '" name="quantity[]" value="'+data[i].quantity+'"/></td>\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="charged'+i+'" name="charged[]" value="'+data[i].charged+'"></td> \n\ 
      <td><input type="hidden" style="width:80px !important;" readonly class="SmallInput" id="patient_visit_statement_id'+i+'" name="patient_visit_statement_id[]" value="'+data[i].patient_visit_statement_id+'"></td> \n\ 
      <td><input type="hidden" style="width:80px color:red!important ;" readonly class="SmallInput" id="patient_id'+i+'" name="patient_payment_id[]" value="'+data[i].patient_payment_id+'"></td> \n\\n\ 
      <td><input type="hidden" style="width:80px !important;" readonly class="SmallInput" id="patient_id'+i+'" name="patient_id[]" value="'+data[i].patient_id+'"></td> \n\ 
      <td><input type="hidden" style="width:80px color:red!important ;" readonly class="SmallInput" id="visit_id'+i+'" name="visit_id[]" value="'+data[i].visit_id+'"></td> \n\n\ 
    </tr> '; 

} 
htmlhead1+='\n\ 
    <th style="width:80px !important;" >Patient Name</th>\n\ 
    <th style="width:80px !important;" > Description</th> \n\ 
    <th style="width:80px !important;" > Amount </th> \n\ 
    <th style="width:80px !important;" >Quantity</th> \n\ 
    <th style="width:80px !important;" >Charged? </th> \n \n \n'; 

$('#tbody1').empty(); 
$('#tbody1').append(htmlhead1); 
$('#tbody1').append(html1); 
$('#tbody1').append(total_price); 

for循環形式在數據庫值的表格填充的動態列表,我要總結的價值的金額並將其視爲總金額/價格。 如何計算循環中的總量並將其附加到#tbody1?

回答

0

追加你有地方你把個人價值打印使用amountTotal量。在它上面,你只需要補充一下,你所擁有的週期只有增加。

之前你對

var total_amount=0; 
    var total_quantity=0; 

,然後循環添加到它

total_amount+=data[i].amount; 
total_quantity+=data[i].quantity; 

然後你只需要編譯TOTAL_PRICE並插入這些變量

0

嘗試用這種

var amountTotal = 0; 
    for(i=0;i<data.length;i++){ 
     amountTotal += data[i].amount; 
     // alert(data[i].stock_id); 
     html1 += '<tr>\n\ 
       <td><input type="text" style="width:80px !important;" readonly id="patient_name' + i + '" name="patient_name[]" value="'+data[i].patient_name+'"/></td>\n\ 
       <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="description' + i + '" name="description[]" value="'+data[i].description+'"/></td>\n\\n\n\ 
       <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="amount' + i + '" name="amount[]" value="'+data[i].amount+'"/></td>\n\ 
       <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="qauntity' + i + '" name="quantity[]" value="'+data[i].quantity+'"/></td>\n\ 
       <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="charged'+i+'" name="charged[]" value="'+data[i].charged+'"></td> \n\ 
       <td><input type="hidden" style="width:80px !important;" readonly class="SmallInput" id="patient_visit_statement_id'+i+'" name="patient_visit_statement_id[]" value="'+data[i].patient_visit_statement_id+'"></td> \n\ 
       <td><input type="hidden" style="width:80px color:red!important ;" readonly class="SmallInput" id="patient_id'+i+'" name="patient_payment_id[]" value="'+data[i].patient_payment_id+'"></td> \n\\n\ 
       <td><input type="hidden" style="width:80px !important;" readonly class="SmallInput" id="patient_id'+i+'" name="patient_id[]" value="'+data[i].patient_id+'"></td> \n\ 
       <td><input type="hidden" style="width:80px color:red!important ;" readonly class="SmallInput" id="visit_id'+i+'" name="visit_id[]" value="'+data[i].visit_id+'"></td> \n\n\ 
     </tr> '; 

    } 

最後由htmlhead1

0

var myData = [1,2,3]; 

var myTotal = 0; //Variable to hold your total 

for(var i=0, len=myData.length; i<len; i++){ 
    myTotal += myData[i]; 
} 

alert(myTotal); // 6 

小提琴演示: - http://jsfiddle.net/rN5m3/

0

試試這個代碼: -

var total=0; 
for(i=0;i<data.length;i++){ 
    total+=data[i].amount; 
    html1 += '<tr>\n\ 
      <td><input type="text" style="width:80px !important;" readonly id="patient_name' + i + '" name="patient_name[]" value="'+data[i].patient_name+'"/></td>\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="description' + i + '" name="description[]" value="'+data[i].description+'"/></td>\n\\n\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="amount' + i + '" name="amount[]" value="'+data[i].amount+'"/></td>\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="qauntity' + i + '" name="quantity[]" value="'+data[i].quantity+'"/></td>\n\ 
      <td><input type="text" style="width:80px !important;" readonly class="SmallInput" id="charged'+i+'" name="charged[]" value="'+data[i].charged+'"></td> \n\ 
      <td><input type="hidden" style="width:80px !important;" readonly class="SmallInput" id="patient_visit_statement_id'+i+'" name="patient_visit_statement_id[]" value="'+data[i].patient_visit_statement_id+'"></td> \n\ 
      <td><input type="hidden" style="width:80px color:red!important ;" readonly class="SmallInput" id="patient_id'+i+'" name="patient_payment_id[]" value="'+data[i].patient_payment_id+'"></td> \n\\n\ 
      <td><input type="hidden" style="width:80px !important;" readonly class="SmallInput" id="patient_id'+i+'" name="patient_id[]" value="'+data[i].patient_id+'"></td> \n\ 
      <td><input type="hidden" style="width:80px color:red!important ;" readonly class="SmallInput" id="visit_id'+i+'" name="visit_id[]" value="'+data[i].visit_id+'"></td> \n\n\ 
    </tr> '; 

} 
html1 += '<tr>\n\ 
     <td> 
     <input type="text" id="total_ans">  
     </td> 
</tr>'; 


var textBox = document.getElementById("total_ans"); 
textBox.value = total.toString();; 

通過這個代碼,你可以得到總的答案,並追加答案TBODY。

我希望它對你有所幫助..