การคำนวณผลรวม เมื่อเปลี่ยนแปลงตัวเลขใน Textbox
จะเป็นการใช้ JavaScript วนลูปตามชื่อของ textbox ( name="price[]" ) เพื่อนำค่าที่ได้บวกเพิ่มเข้าไปในตัวแปร sum แล้วก็แสดงผลในช่องผลรวม sumprice
<html>
<head>
<meta charset="UTF-8">
<script language="JavaScript">
function fncSum()
{
var num = ''; var sum = 0;
for(var i=0;i<document.frmprice['price[]'].length;i++){
num = document.frmprice['price[]'][i].value;
if(num!=""){
sum += parseFloat(num);
}
}
document.frmprice.sumprice.value = sum; }
</script>
</head>
<body>
<h2>การคำนวณผลรวม เมื่อเปลี่ยนแปลงตัวเลข</h2>
<form name="frmprice" method="POST">
<table>
<?php for($i=1;$i<=5;$i++){ ?>
<tr>
<td><input type="text" name="price[]" id="price[]" onkeyup="fncSum();"></td>
</tr>
<?php } ?>
<tr>
<td>
<label>Total : </label>
<input type="text" name="sumprice" readonly></td>
</tr>
</table>
</form>
<p>
ที่มา : http://www.thaicreate.com/php/forum/127342.html
</p>
</body>
</html>
จากกระทู้ : http://www.thaicreate.com/php/forum/127342.html
PHP CI MANIA - PHP Code Generator
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม เขียนโปรแกรมง่ายและสะดวกขึ้น
สนใจสั่งซื้อราคาสุดคุ้ม >> http://fastcoding.phpcodemania.com/