菜菜问题,大家帮帮忙看看要怎么改???

AskValue 2005-07-05 11:23:53
功能其实很简单啦,但为什么我这样写不行呢???

<html>
<head>
<script language>
function calc(theForm,theText){
var totalText = parseFloat(document.forms(theForm).elements('total').value);
var textValue = parseFloat(theText.value);
totalText += textValue;
document.all('total').value = totalText;
}
</script>
</head>

<body>
<form method="POST" name="f_count">
<p><input type="text" name="T1" onchange="calc('f_count',this)" size="20"><br>
        +<br>
<input type="text" name="T2" onchange="calc('f_count',this)" size="20"><br>
        +<br>
<input type="text" name="T4" onchange="calc('f_count',this)" size="20"><br>
        =<br>
<input type="text" name="total" size="20"></p>
</form>
</body>
</html>
...全文
71 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
AskValue 2005-07-05
  • 打赏
  • 举报
回复
谢谢这位大哥!
按我的那个功能做,不知道还有没有办法??我想把这个函数写得更灵活一点.
JK_10000 2005-07-05
  • 打赏
  • 举报
回复
<html>
<head>
<script language>
function calc(frmObj){
var totalText =0;
totalText += frmObj.T1.value*1;
totalText += frmObj.T2.value*1;
totalText += frmObj.T4.value*1;
frmObj.total.value = totalText;
}
</script>
</head>

<body>
<form method="POST" name="f_count">
<p><input type="text" name="T1" onchange="calc(this.form)" size="20"><br>
        +<br>
<input type="text" name="T2" onchange="calc(this.form)" size="20"><br>
        +<br>
<input type="text" name="T4" onchange="calc(this.form)" size="20"><br>
        =<br>
<input type="text" name="total" size="20"></p>
</form>
</body>
</html>
JK_10000 2005-07-05
  • 打赏
  • 举报
回复
原贴代码运行不对的原因:
当total值为“”时,parseFloat的结果是NaN
如果将四个输入框的初始值设为0,情形可能就向楼主的想象近了一步(虽说还是有较远距离)。

87,904

社区成员

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

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