<html>
<head></head>
<script type="text/javascript">
function total() {
if (document.form.t1.value == cash) {
var onet = document.form.one.value
} else {
var onet = 0;
}
if (document.form.t2.value == cash) {
var twot = document.form.two.value
} else {
var twot = 0;
}
if (document.form.t3.value == cash) {
var threet = document.form.three.value
} else {
var threet = 0;
}
document.form.sum.value = parseFloat(onet) + parseFloat(twot) + parseFloat(threet);
}
</script>
<body>
<form name="form">
<ol>
<li>Amount :
<input type="text" name="one" onchange="total()" />Type :
<select name="t1" onchange="total()">
<option value="cash">cash</option>
<option value="cheque">cheque</option>
</select>
<br/>
</li>
<li>Amount :
<input type="text" name="two" onchange="total()" />Type :
<select name="t2" onchange="total()">
<option value="cash">cash</option>
<option value="cheque">cheque</option>
</select>
<br/>
</li>
<li>Amount :
<input type="text" name="three" onchange="total()" />Type :
<select name="t3" onchange="total()">
<option value="cash">cash</option>
<option value="cheque">cheque</option>
</select>
<br/>
</li>
</ol>Total :
<input type="text" name="sum" />
</form>
</body>
</html>
在上面的代码中,我想做的是:如果用户从选项中选择现金,然后转到下一个字段,它将自动添加到总和中,如果选择了支票,那么它的值将是零或者它将是未添加。因此,如果用户选择两个 vcash 和一个支票,则将添加带有选项 cash 的行,结果将是它们的总和但没有值出现,它将显示为空白。