但我想总结两个文本字段,它们会在“总计”文本字段中自动显示答案。尝试onkeyup和onchange。但它不会工作。这是我的HTML代码:
<input type="text" value="$0.00" size="6" maxlength="6" name="form[family_home]" id="family_home" onfocus="if (this.value == '$0.00') {this.value=''}" class="assets rsform-input-box">
<input type="text" value="$0.00" size="6" maxlength="6" name="form[home_contents]" id="home_contents" onfocus="if (this.value == '$0.00') {this.value=''}" class="assets rsform-input-box">
<input type="text" value="Total" size="20" name="form[Total]" id="Total" onchange="calculateText();" readonly="readonly" class="rsform-input-box">
这是脚本:
<script type="text/javascript">
function calculateText(){
var op1=document.getElementById('family_home');
var op2=document.getElementById('home_contents');
var result=document.getElementById('Total');
if(op1.value=="$0.00" || op1.value!=parseFloat(op1.value)) op1.value=0;
if(op2.value=="$0.00" || op2.value!=parseFloat(op2.value)) op2.value=0;
result.value=0;
result.value=parseFloat(result.value);
result.value=parseFloat(result.value)+parseFloat(op1.value)+parseFloat(op2.value);
</script>
答案 0 :(得分:1)
您需要将onchange事件放在您正在修改的输入上:family_home和home_contents