Javascript 从Jquery中foreach生成的表计算总价
我有以下代码:Javascript 从Jquery中foreach生成的表计算总价,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我有以下代码: 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].p
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循环表单在数据库中的值表中填充一个动态列表,我想对amount的值求和,并将其视为total amount/price。
如何计算循环中的总金额并将其附加到tbody1 试试这个
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中添加数字,使用amountTotal打印金额。您可以在某个地方将各个值相加. 在它上面,你只需要加上,你所拥有的循环,只需要加上 在你骑自行车之前
var total_amount=0;
var total_quantity=0;
然后再加上
total_amount+=data[i].amount;
total_quantity+=data[i].quantity;
然后你只需要编译TOTAL_PRICE并插入这些变量
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
小提琴演示:-尝试以下代码:-
通过这段代码,您可以得到总答案并将答案附加到tbody中
我希望这对你有帮助
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();;