ผมอยากจะรวมค่าใน textbox ในคอลั่มสุดท้ายทั้งหมดต้องทำยังไงครับ
คือผมเอาโค้ดมาแปลงรวมกัน แล้วผมอยากจะให้ textbox ในคอลั่มสุดท้ายทั้งหมดรวมกันต้องทำยังไงครับ ผมแก้มานานแล้ว T_Tแต่ไม่เข้าใจ Js ผมรู้ช่วยเขียนให้ที่ครับ
Code
<meta charset="utf-8">
<script type="text/javascript">
function add_report_row()
{
var container = document.getElementById('inputboxes_description');
var blank = document.createTextNode(" ");
var br_ = document.createElement("br");
var report_row = document.createElement("div");
var delete_link = document.createElement("a");
var nums = parseInt(document.getElementById('nums').value)+1;
document.getElementById('nums').value = nums;
var rowId = 'row'+nums;
var descName = "desc["+nums+"]";
var widName = "wid["+nums+"]";
var highName = "high["+nums+"]";
var totalName = "total["+nums+"]";
function textarea_add(id_,cols_)
{
var textArea = document.createElement("textarea");
textArea.setAttribute('id',id_);
textArea.setAttribute('names',id_);
textArea.setAttribute('cols',cols_);
return textArea;
}
report_row.setAttribute('id',rowId);
report_row.appendChild(textarea_add(descName,'25'));
report_row.appendChild(blank);
report_row.appendChild(textarea_add(widName,'15'));
report_row.appendChild(blank);
report_row.appendChild(textarea_add(highName,'15'));
report_row.appendChild(blank);
report_row.appendChild(textarea_add(totalName,'15'));
report_row.appendChild(blank);
delete_link.setAttribute("href","javascript:;");
delete_link.onclick = function() {del_report_row(rowId);}
delete_link.innerHTML = '[ลบ]';
report_row.appendChild(delete_link);
report_row.appendChild(br_);
container.appendChild(report_row);
document.getElementById(widName).onkeyup = function ()
{
document.getElementById(totalName).value = document.getElementById(widName).value *1 * document.getElementById(highName).value * 1;
if( document.getElementById(totalName).value=="NaN"){ document.getElementById(totalName).value = "0";}
}
document.getElementById(highName).onkeyup= function ()
{
document.getElementById(totalName).value = document.getElementById(widName).value *1 * document.getElementById(highName).value * 1;
if( document.getElementById(totalName).value=="NaN"){ document.getElementById(totalName).value = "0";}
}
}
/*function fncCal()
{
var sum = 0;
for(var i = 0; i<totalName.length; i++){
if (totalName[i] instanceof Array) { sum += fncCal(totalName[i]);}
if (totalName[i] === Math.round(totalName[i])) {sum += totalName[i];}
}
return sum;
}*/
function del_report_row(rows)
{
var container = document.getElementById('inputboxes_description');
if (confirm('delete rows?'))
{
container.removeChild(document.getElementById(rows));
}
}
</script>
<form name="formx">
<input id="nums" type="hidden" value="0" />
<table width="780" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#e4dabf" class="bold1">
<tr align="center" valign="middle" class="bold1">
<td width="211" bgcolor="#e4dabf">Object</td>
<td width="155" bgcolor="#e4dabf">Width</td>
<td width="150" bgcolor="#e4dabf">Height</td>
<td width="150" bgcolor="#e4dabf">Total</td>
</tr>
<tr align="center">
<td height="30" colspan="4" align="center" valign="top">
<div id="inputboxes_description">
</div>
<br/>
<button onclick="add_report_row();return false;"/>เพิ่มวัสดุ</button>
Total :<input type="text" name="totalcal" id="totalcal" value="" onfocus="fncCal();">
</td>
</tr>
</table>
</form>
<script language="javascript">
<?php foreach(array(1,2,3,4,5) as $n): ?>
add_report_row();
<?php endforeach; ?>
</script>
Tag : HTML/CSS, JavaScript, JAVA
Date :
2015-06-17 16:30:25
By :
blucifer
View :
1217
Reply :
2
ใช้ jQuery จะง่ายกกว่าครับ ลองดูกระทู้นี้ครับ
[ Example ] : jQuery calculator (Sum Total Muti-Textbox)
Date :
2015-06-18 14:41:20
By :
mr.win
Load balance : Server 05