2011-04-26 40 views
-1

我試圖讓小計算器jQuery的,但不知道從哪裏開始的問題,請幫助我有jQuery函數

<body> 

<script> 
$(document).ready(function(){ 

    function show_total(){ 
     document.write('something'); 
    } 

    $("#form1").submit(function(){ 
     $("#total").append('<p>show total</p>'); 
}); 

}); 
</script> 



<form id="form1"> 
num1 <input type="text" id="in1" size=5 /> + 
num2 <input type="text" id="in2" size=5 /> 
<input type="submit"> 



</form> 

<div id="total"></div> 
+0

什麼是你想通過附加*的最後一個div內

秀總

*實現? – dmarucco 2011-04-26 07:18:34

+0

從您的代碼中移除document.write()開始,您不能在加載頁面後使用它。 – 2011-04-26 07:19:41

回答

2
<script> 
$(document).ready(function(){ 

    $("#form1").submit(function(){ 
     var total = parseInt($("#in1").val())+parseInt($("#in2").val()); 
     //or some other calculation 
     $("#total").append("<p>"+total+"</p>")); 
    }); 

}); 
</script> 
0

你嘗試谷歌?

退房此樣本中,抵押貸款計算器:SOURCE

<!-- if you don't have jquery --> 
<script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js'></script> 

<!--The following formula is used to calculate the fixed monthly payment (P) required to fully amortize a loan of L dollars over a term of n months at a monthly interest rate of c. [If the quoted rate is 6%, for example, c is .06/12 or .005]. 

P = L[c(1 + c)^n]/[(1 + c)^n - 1]--> 
<h3>Mortgage Calculator</h3> 
    <form > 
<p> <input type="text" name="mcPrice" id="mcPrice" class="mortgageField" /> 
Sale price ($) 
</p> 
<p> <input type="text" name="mcDown" id="mcDown" class="mortgageField" /> 
Down payment (%)</p> 
<p> <input type="text" name="mcRate" id="mcRate" class="mortgageField" /> 
Interest Rate (%)</p> 
<p> <input type="text" name="mcTerm" id="mcTerm" class="mortgageField" /> 
Term (years)</p> 
<button class="smallButton" id="mortgageCalc" onclick="return false">Calculate Monthly Payment</button> 
    <input type="text" name="mcPayment" id="mcPayment" class="mortgageAnswer" /> 
</form> 
<script type="text/javascript"> 
$("#mortgageCalc").click(function(){ 
var L,P,n,c,dp; 
L = parseInt($("#mcPrice").val()); 
n = parseInt($("#mcTerm").val()) * 12; 
c = parseFloat($("#mcRate").val())/1200; 
dp = 1 - parseFloat($("#mcDown").val())/100; 
L = L * dp; 
P = (L*(c*Math.pow(1+c,n)))/(Math.pow(1+c,n)-1); 
if(!isNaN(P)) 
{ 
$("#mcPayment").val(P.toFixed(2)); 
} 
else 
{ 
$("#mcPayment").val('There was an error'); 
} 
return false; 
}); 
</script>