我有jquery函数的问题

时间:2011-04-26 07:11:03

标签: 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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>