0
<form name="cost">
    <table border="1">
        <tr>
            <td>Cost</td>
            <td><input type="text" name="cost" /></td>
        </tr>
        <tr>
            <td>Discount</td>
            <td><input type="text" name="discount" /> (<span id="discount2"></span>)%</td>
        </tr>
        <tr>
            <td>Net Cost</td>
            <td><input type="text" name="net" /></td>
        </tr>
    </table>
</form>

大家好,我真的需要javascript编程的帮助。表格的过程是:

  1. 在成本中放入一些
  2. 然后放多个折扣(自动计算 span#discount2=cost*discount/100)
  3. 净成本自动更新 = 成本折扣

我尝试了很多次,但没有运气加上缺乏 javascript 知识。请帮忙。

4

1 回答 1

0

首先,创建一个函数来做到这一点

function calculateDiscount()
{
var cost = document.getElementById('cost').value;
var discount = document.getElementById('discount').value;
//do the math
var net = cost-discount;
//update
document.getElementById('discount2').innerHTML = cost*(discount/100);
document.getElementById('net').value = net;
}

您还需要更新您的 html,将 ID 添加到所有元素并调用该函数

<form name="cost">
<table border="1">
<tr><td>Cost</td><td><input type="text" id="cost" name="cost" onChange="calculateDiscount(); return false;" /></td></tr>
<tr><td>Discount</td><td><input onChange="calculateDiscount(); return false;" type="text" id="discount" name="discount" /> (<span id="discount2"></span>)%</td></tr>
<tr><td>Net Cost</td><td><input type="text" name="net" id="net" /></td></tr>
</table>
</form>
于 2012-06-22T19:13:59.943 回答