但我想總結兩個文本框,它們將自動顯示「Total」文本框中的答案。試過onkeyup和onchange。但它不會工作。這裏是我的HTML代碼:Javascript,Sum Textfield不會顯示SUM
<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>
你有將處理程序綁定到其值改變的輸入字段,而不是「#總計」。 –