在这里,我发布了我的 javascript 和 html 编码。如果用户单击计算总计按钮,它应该添加所有值并显示在文本框中。这完美无缺。但问题是,如果用户输入(此处总共 7 行 ftotal、fttotal1、fttotal2、fttotal3、fttotal4、fttotal5、fttotal6)5 行值,并且当他们单击计算总计按钮时,文本字段显示为 NaN。我想如果他们没有输入几行,当他们点击按钮时,剩余的行将被计算并显示在文本框中......???
JavaScript
function calfinal(frm) {
var ftotal = frm.ftotal.value
var ftotal1 = frm.ftotal1.value
var ftotal2 = frm.ftotal2.value
var ftotal3 = frm.ftotal3.value
var ftotal4 = frm.ftotal4.value
var ftotal5 = frm.ftotal5.value
var ftotal6 = frm.ftotal6.value
var calc = 0
ftotal = parseFloat(ftotal)
ftotal1 = parseFloat(ftotal1)
ftotal2 = parseFloat(ftotal2)
ftotal3 = parseFloat(ftotal3)
ftotal4 = parseFloat(ftotal4)
ftotal5 = parseFloat(ftotal5)
ftotal6 = parseFloat(ftotal6)
answer = ftotal + ftotal1 + ftotal2 + ftotal3 + ftotal4 + ftotal5 + ftotal6
frm.calc.value = answer
}
HTML
<tr>
<td height="30" class="tabform"><input type="text" size="10" name="ftotal" /></td>
</tr>
<tr>
<td height="30" class="tabform"><input type="text" size="10" name="ftotal1"/></td>
</tr>
<tr>
<td height="30" class="tabform"><input type="text" size="10" name="ftotal2"/></td>
</tr>
<tr>
<td height="30" class="tabform"><input type="text" size="10" name="ftotal3"/></td>
</tr>
<tr>
<td height="30" class="tabform"><input type="text" size="10" name="ftotal4"/></td>
</tr>
<tr>
<td height="30" class="tabform"><input type="text" size="10" name="ftotal5"/></td>
</tr>
<tr>
<td height="30" class="tabform"><input type="text" size="10" name="ftotal6"/></td>
</tr>
<tr>
<td height="15"><input type="button" value="Calculate Total" onClick="calfinal(this.form);" /></td>
<td height="15" align="center"><input type="text" size="10" name="calc" readonly="readonly" /></td>
</tr>