正如你所看到的那样,我在一个表上过滤的(总)文本框中已有一个值...如果我在(得分)文本框上键入一个数字,它将自动添加总数的当前值(文本框)...任何人都可以帮我解决我的代码错误,因为它不能正常工作......不会计算。
html代码:
<form id="frm" name="frm" />
<table>
<tr>
<td>
Name: <br />
<input type="text" name="name" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" readonly /> <br />
</td>
<td>
Score 1: <br />
<input type="text" name="optA" value="" onkeypress="return isnumeric(event)" onchange="optTotal()" /> <br />
</td>
<td>
Score 2: <br />
<input type="text" name="optB" value="" onkeypress="return isnumeric(event)" onchange="optTotal()" /> <br />
</td>
<td>
Score 3: <br />
<input type="text" name="optC" value="" onkeypress="return isnumeric(event)" onchange="optTotal()" /> <br />
</td>
<td>
Score 4: <br />
<input type="text" name="optD" value="" onkeypress="return isnumeric(event)" onchange="optTotal()" /> <br />
</td>
<td>
Total: <br />
<input type="text" name="totals" value="<?php if(empty($total[0])){$total[0] = array(NULL);}else{echo $total[0];} ?>" readonly onKeyUp="optTotal()" /> <br />
</td>
</form>
要计算的脚本代码:
<script>
function optTotal() {
var a1 = document.forms[0].optA;
var b1 = document.forms[0].optB;
var c1 = document.forms[0].optC;
var d1 = document.forms[0].optD;
var xtotal = document.forms[0].totals;
if (a1.value && a1.value != "")
a1 = parseFloat(a1.value);
else
a1 = 0;
if (b1.value && b1.value != "")
b1 = parseFloat(b1.value);
else
b1 = 0;
if (c1.value && c1.value != "")
c1 = parseFloat(c1.value);
else
c1 = 0;
if (d1.value && d1.value != "")
d1 = parseFloat(d1.value);
else
d1 = 0;
if (xtotal.value && xtotal.value != "")
xtotal = parseFloat(xtotal.value);
else
xtotal = 0;
var total = a1 + b1 + c1 + d1 + xtotal;
document.forms[0].total.value = total;
}
</script>
答案 0 :(得分:0)
试试这个
set id for total input e.g TOTAL
document.getElementById('TOTAL').setAttribute('value',total)
或将此行total
更改为totals
document.forms[0].totals.value = total
答案 1 :(得分:0)
脚本JS中存在错误:
不是:
document.forms[0].total.value = total;
可是:
document.forms[0].totals.value = total;
答案 2 :(得分:-1)
1确保您的“isnumeric”函数正确运行且可以访问,因此它不会中止脚本。
2您的“总计”名称不正确:
document.forms[0].total.value = total;
将其更改为:
document.forms[0].totals.value = total;
3使用浏览器控制台查看错误!