我对我的程序的使命是当在文本框中过滤记录时,如果我更改数字,它将自动更改总数并在(总)文本框中计算...任何人都可以帮我解决我的代码错误吗?我通常不会循环文本框,因为我有计划。
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 onClick="optTotal()" /> <br />
</td>
<td>
Score 1: <br />
<input type="text" name="optA" value="<?php if(empty($score1[0])){$score1[0] = array(NULL);}else{echo $score1[0];} ?>" onkeypress="return isnumeric(event)" onClick="optTotal()" /> <br />
</td>
<td>
Score 2: <br />
<input type="text" name="optB" value="<?php if(empty($score2[0])){$score2[0] = array(NULL);}else{echo $score2[0];} ?>" onkeypress="return isnumeric(event)" onClick="optTotal()" /> <br />
</td>
<td>
Score 3: <br />
<input type="text" name="optC" value="<?php if(empty($score3[0])){$score3[0] = array(NULL);}else{echo $score3[0];} ?>" onkeypress="return isnumeric(event)" onClick="optTotal()" /> <br />
</td>
<td>
Score 4: <br />
<input type="text" name="optD" value="<?php if(empty($score4[0])){$score4[0] = array(NULL);}else{echo $score4[0];} ?>" onkeypress="return isnumeric(event)" onClick="optTotal()" /> <br />
</td>
<td>
Total: <br />
<input type="text" name="total" 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.querySelector('select[name="optA"]');
var b1 = document.querySelector('select[name="optB"]');
var c1 = document.querySelector('select[name="optC"]');
var d1 = document.querySelector('select[name="optD"]');
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;
document.getElementById("total").value = parseFloat(a1)+parseFloat(b1)+parseFloat(c1)+parseFloat(d1);
}
</script>
问题是,如果我将(optA)上的文本框中的数字更改为(optD),则无法计算。
答案 0 :(得分:2)
在onchange事件
上使用它 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;
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;
var total = a1 + b1 + c1+d1;
document.forms[0].total.value = total;
}