我正在为练习写一个在线贷款计算器。数据处理都是在客户端完成的,但JSFiddle要我使用POST
。为什么是这样?这可能与当地点击计算按钮时,表格只是清除了吗? JSFiddle中的代码:http://jsfiddle.net/TJonS/CuzSM/
另外,为什么点击按钮不计算?我已多次尝试调试,但Chrome没有显示任何错误。
使用Javascript:
function calculate(){
//get the elements
var amount = document.getElementById("amount");
var rate = document.getElementById("rate");
var duration = document.getElementById("duration");
//get the values of the elements
var a = parseFloat(amount.value);
var r = parseFloat(rate.value);
var d = parseFloat(duration.value);
//grab the outputable (readable(ha ha:))) variables
var principal = a;
var interest = r/100/12;
var time = d *12;
//now the calculation variables
var x = Math.pow(1+interest, payments);
var monthlypay = (principal*x*interest)/(x-1);
//if the result is a finite number, then display it. Else we're messed up!
if (isFinite(monthlypay)) {
//fill in outputs
payment.innerHTML = monthlypay.toFixed(2);
total.innerHTML = (monthlypay * payments).toFixed(2);
totalinterest.innerHTML = ((monthlypay*payments)-principal).toFixed(2);
//save the variables
save(amount.value, rate.value,duration.value, a.value, r.value, d.value, principal.value, total.value, totalinterest.value)
}
//else just make the outputs blank as can be.
else {
payment.innerHTML = "";
total.innerHTML = "";
totalinterest.innerHTML = "";
}
}
答案 0 :(得分:3)
刚刚放
return false;
在您的计算功能的底部,停止执行表单帖子的默认onClick行为(按钮)。
...也
是你的if语句“if(isFinite(monthlypay)){”实际上得到关注? 这似乎每次都在抹去价值观。
else {
payment.innerHTML = "";
total.innerHTML = "";
totalinterest.innerHTML = "";
}
检查“isFinite(monthlypay)”函数是否返回true。 (很可能永远不会)
答案 1 :(得分:0)
点击时没有帖子的按钮:
<input type="button" onclick="dosomething();" value="my button text" />