我是 javascript 新手和新手程序员,所以这可能是一个非常容易回答的问题。我想遍历 x 个字段的值并将它们相加以在最终字段中显示总和。如果我显式调用每个字段,我可以执行此功能,但我想将其抽象化,以便我可以处理灵活数量的字段。这是我想出的示例代码(这对我不起作用)。我哪里错了?
<html>
<head>
<script type="text/javascript">
function startAdd(){
interval = setInterval("addfields()",1);
}
function addfields(){
a = document.addition.total.value;
b = getElementByTagName("sum").value;
for (i=0; i<=b.length; i++){
a+=i;
}
return a;
}
function stopAdd(){
clearInterval(interval);
}
</script>
</head>
<body>
<form name="addition">
<input type="text" name="sum" onFocus="startAdd();" onBlur="stopAdd;">
+ <input type="text" name="sum" onFocus="startAdd();" onBlur="stopAdd;">
= <input type="text" name ="total">
</form>
</body>
</html>