<SCRIPT LANGUAGE="JavaScript">
<!--
function multiplication(n) {
  var am = document.forms[0].elements["Amount" + n];
  var pr = document.forms[0].elements["Price" + n];
  if(am.value!=""&&(am.value/1==am.value)&&pr.value!=""&&(pr.value/1==pr.value)) 
  var aa=(am.value/1)*(pr.value/1);
  document.getElementById("Investments"+n).innerHTML=aa.toFixed(2);
}
//-->
</SCRIPT><form>
<table border="1" width="300"> 
<tr>
 <td width="73"><input name="Amount1" type="text" id="Amount1" value="0.00"  size="8" onpropertychange="multiplication(1);"/></td>
              <td width="75"><input name="Price1" type="text" id="Price1" value="0.00"  size="8"  onpropertychange="multiplication(1);"/></td>
              <td  width="68" id="Investments1"></td>
</tr>
<tr>
 <td width="73"><input name="Amount2" type="text" id="Amount2" value="0.00"  size="8" onpropertychange="multiplication(2);"/></td>
              <td width="75"><input name="Price2" type="text" id="Price2" value="0.00"  size="8"  onpropertychange="multiplication(2);"/></td>
              <td  width="68" id="Investments2"></td></tr>
<tr>
  <td width="73"><input name="Amount3" type="text" id="Amount3" value="0.00"  size="8" onpropertychange="multiplication(3);"/></td>
              <td width="75"><input name="Price3" type="text" id="Price3" value="0.00"  size="8"  onpropertychange="multiplication(3);"/></td>
              <td  width="68" id="Investments3"></td>
</tr>
</table>
</form>
Amount文本和Price文本,必须限定他小数位数不超过2位
显示效果能不能是整数的时候以.00填充小数位
小数的时候,超过2位就截取掉

解决方案 »

  1.   


     <SCRIPT LANGUAGE="JavaScript">
    <!--
    function multiplication(n) {
      var am = document.forms[0].elements["Amount" + n];
      var pr = document.forms[0].elements["Price" + n];
      if(am.value!=""&&(am.value/1==am.value)&&pr.value!=""&&(pr.value/1==pr.value)) 
      var aa=(am.value/1)*(pr.value/1);
      document.getElementById("Investments"+n).innerHTML=aa.toFixed(2);
    }
    function fix(obj){
       obj.value = Math.round(obj.value * 100) / 100;
       var v = parseFloat(obj.value).toFixed(2);
       if(isNaN(v)) obj.value = "0.00";
       else obj.value = v;
    }
    //-->
    </SCRIPT>
    <form>
    <table border="1" width="300"> 
    <tr>
     <td width="73"><input name="Amount1" type="text" id="Amount1" value="0.00" size="8" onblur="fix(this)" onpropertychange="multiplication(1);"/></td>
                  <td width="75"><input name="Price1" type="text" id="Price1" value="0.00"  size="8" onblur="fix(this)" onpropertychange="multiplication(1);"/></td>
                  <td  width="68" id="Investments1"></td>
    </tr>
    <tr>
     <td width="73"><input name="Amount2" type="text" id="Amount2" value="0.00"  size="8" onblur="fix(this)" onpropertychange="multiplication(2);"/></td>
                  <td width="75"><input name="Price2" type="text" id="Price2" value="0.00"  size="8" onblur="fix(this)" onpropertychange="multiplication(2);"/></td>
                  <td  width="68" id="Investments2"></td></tr>
    <tr>
      <td width="73"><input name="Amount3" type="text" id="Amount3" value="0.00"  size="8" onblur="fix(this)" onpropertychange="multiplication(3);"/></td>
                  <td width="75"><input name="Price3" type="text" id="Price3" value="0.00"  size="8" onblur="fix(this)" onpropertychange="multiplication(3);"/></td>
                  <td  width="68" id="Investments3"></td>
    </tr>
    </table>
    </form>