|
|
|
รบกวนสอบถาม javascript คำนวนค่า กรณีที่ป้อนข้อมูลยังไม่ครบแต่ผลลัพธ์แสดงเป็น NaN อยากให้ขึ้นเป็น 0 หรือ ว่างเปล่าแทน |
|
|
|
|
|
|
|
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="th" xml:lang="th">
<head>
<meta http-equiv="content-type" content="text/html; charset=tis-620" />
<title>calculate number</title>
<script type="text/javascript">
//<![CDATA[
window.onload=function(){
var f = document.getElementById('box');
function calA(i){
var d = function (){
var k = f['k[]'][i];
var p = f['p[]'][i];
var a = f['a[]'][i];
k.value = k.value.replace(/[^\d\.]/g,'');
p.value = p.value.replace(/[^\d\.]/g,'');
a.value = (k.value*1) * (p.value*1);
if (a.value <= 0){
a.style.color = 'red';
} else {
a.style.color = 'black';
}
var sum = 0;
var j = 0;
while(j < f['a[]'].length){
var t = f['a[]'][j];
sum += (t.value*1);
j++;
}
document.getElementById('c').value = sum;
}
return d;
}
for(var i=0;i<f['k[]'].length;i++){
f['k[]'][i].onkeyup = calA(i);
f['p[]'][i].onkeyup = calA(i);
}
}
//]]>
</script>
</head>
<body>
<form method="post" action="?" id="box">
<input type="text" name="a" id="a" value="k" disabled="disabled" />
<input type="text" name="p" id="p" value="p" disabled="disabled" />
<input type="text" name="k" id="k" value="a" disabled="disabled" />
<?php echo '<br'.' />';?>
<?php foreach(range(1,5) as $m):?>
<input type="text" name="k[]" value="" />
<input type="text" name="p[]" value="" />
<input type="text" name="a[]" value="" />
<?php echo '<br'.' />';?>
<?php endforeach;?>
<input type="text" name="c" id="c" value="" />
</form>
</body>
</html>
ขอบคุณครับ
|
|
|
|
|
Date :
2013-01-25 14:45:48 |
By :
nook563 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|