我有以下代码:
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 :(得分: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
答案 1 :(得分:0)
您可以在某处添加各个值。在它上面,你只需要添加你所拥有的循环,只有添加。
在你的周期之前
var total_amount=0;
var total_quantity=0;
然后添加到它
total_amount+=data[i].amount;
total_quantity+=data[i].quantity;
然后你只需要编译TOTAL_PRICE并插入这些变量
答案 2 :(得分: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/
答案 3 :(得分: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中附加答案。
我希望它对你有所帮助..