截取输入小数长度的问题

dreat 2009-06-08 09:21:52

<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位就截取掉


...全文
33 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dreat 2009-06-09
  • 打赏
  • 举报
回复
在此感谢hookee ,结贴
hookee 2009-06-08
  • 打赏
  • 举报
回复

<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>

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧