我有一个自动更新总和表格,可以完美运行 3 个输入。但我需要再计算一个输入。我尝试添加一个字段并尝试模仿脚本但没有运气。请帮忙。
function formatDollar(num) {
var p = num.toFixed(2).split(".");
return "" + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
return num + (i && !(i % 3) ? "," : "") + acc;
}, "");
}
//auto sum
function calculateDiscount()
{
var cost = document.getElementById('insr_total1').value.replace(/[^0-9]+/i,"");
cost = cost.replace(/[^0-9]+/i,"");
var discount = document.getElementById('insr_discount').value;
//do the math
var net = cost-discount;
//update
document.getElementById('insr_discount2').innerHTML = formatDollar(cost*(discount/100));
document.getElementById('insr_total3').value = formatDollar(cost-cost*(discount/100));
}
这是我使用的html:
<table>
<tr>
<td align="left" valign="top">Cost1</td>
<td align="left" valign="top"><input type="text" name="insr_total1" id="insr_total1" size="17" maxlength="15" onKeyup="calculateDiscount(); return false;" /> USD</td>
</tr>
<tr>
<td align="left" valign="top">Discount</td>
<td align="left" valign="top"><input type="text" name="insr_discount" id="insr_discount" size="4" maxlength="3" onkeyup="calculateDiscount(); return false;" /> % (<span id="insr_discount2" style="color:green; font-size: 13px;"></span> USD)</td>
</tr>
<tr>
<td align="left" valign="top">Cost 2</td>
<td align="left" valign="top"><input type="text" name="insr_total2" id="insr_total2" size="17" maxlength="15" /> USD</td>
</tr>
<tr>
<td align="left" valign="top">Overall</td>
<td align="left" valign="top"><input type="text" name="insr_total3" id="insr_total3" size="17" readonly maxlength="15" /> USD</td>
</tr>
</table>
好吧,我需要从 insr_total3 中减去“insr_total2”。无论我尝试过,它总是会出错。感谢所有回复。