客户端自动累加

    有的时候需要在输入数量的同时,实时计算出输入的总和,这就要借助于Javascript

<script language="JavaScript">
function count() 
{     
        
var one = document.thisform.one.value;
        
var two = document.thisform.two.value;
        
if ((one!="")&&(two!=""))
        {
                document.thisform.total.value 
= parseInt(one) + parseInt(two);
        }
        
else
        {
                document.thisform.total.value 
= "";
        }  
}
</script>

       需要注意的是, 取得的one和two的值,由于是字符型, 需要先将one , two转换成数字类型. 否则 计算出的结果会变成1+1=11
所用到的都是客户端控件

<table width="100%" cellspacing="1" cellpadding="5" align="center" class="bg_tablemain">
 
<form action="" method="POST" name="thisform">
 
<tr>
 
<td align="right">一月數量</td>
 
<td><input type="text" name="one" size="4" value="" onKeyUp="value=value.replace(/\D+/g,'');
    count();"
></td>
 
<td align="right">2月數量</td>
 
<td><input type="text" name="two" size="8" onKeyUp="count();" ></td>
 
<td align="right">總數量:</td>
 
<td><input type="text" name="total" size="12" readonly value="" style="color: #CC0000"></td>
 
</tr>
 
</form>
 
</table> 
原文地址:https://www.cnblogs.com/maoniu602/p/1315944.html