我是HTML5和Jquery的入门者。我已经建立了一段代码来根据“期限和利息”计算贷款金额。但是看来我自己不行。数字不会自动计算并保持为0。感谢您的帮助。谢谢大家。
这是用于在Wordpress网站上粘贴的脚本
<script>
function pmt(rate_per_period, number_of_payments, present_value, future_value, type){
if(rate_per_period != 0.0){
// Interest rate exists
var q = Math.pow(1 + rate_per_period, number_of_payments);
return -(rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
} else if(number_of_payments != 0.0){
// No interest rate, but number of payments exists
return -(future_value + present_value) / number_of_payments;
}
return 0;
}
function calcLoanezi(){
var interest;
if ($( "#invoice" ).val() =< 5000) {interest = 0.12}
else if ($( "#invoice" ).val() >= 5001 && $( "#invoice" ).val() <= 10000) {interest = 0.10}
else if ($( "#invoice" ).val() >= 10001 && $( "#invoice" ).val() <= 20000) {interest = 0.08}
else if ($( "#invoice" ).val() >= 20001 && $( "#invoice" ).val() <= 35000) {interest = 0.0725}
else if ($( "#invoice" ).val() >= 35001 && $( "#invoice" ).val() <= 60000) {interest = 0.0695}
else if ($( "#invoice" ).val() >= 60001 && $( "#invoice" ).val() <= 100000) {interest = 0.0675}
var months; // Lifetime of loan (in years)
if ($("#term").val() >=60) {interest = interest+0.01}
present = $( "#invoice" ).val(), // Present value of loan
future = 0, // Future value of loan
beginning = 0; // Calculated at start of each period
brokerage = present * 0.06;
var payment = -pmt((interest / 12), months, (Number(brokerage) + Number(present)), future, beginning);
var payment_2dp_exgst = payment.toFixed(2);
var payment_2dp_gst = ((payment*1.1)-payment).toFixed(2);
var payment_2dp_incgst = (Number(payment_2dp_exgst) + Number(payment_2dp_gst)).toFixed(2);
$( "#monthlyex" ).val("$"+payment_2dp_exgst);
$( "#monthlygst" ).val("$"+payment_2dp_gst);
$( "#monthlyinc" ).val("$"+payment_2dp_incgst);
}
$( document ).ready(function() {
$( "#invoice" ).keyup(function(){
calcLoanezi();
});
$("#term").on('change', function() {
calcLoanezi();
});
});
</script>
它没有显示任何结果
答案 0 :(得分:0)
下面的代码段应该可以正常工作(从技术上讲,但不是没有HTML,或者至少没有模型输入)。我给了它更多的结构,关注点分离了。
如果您提供HTML,则可以修改JS代码段以使其正常工作(目前,由于未附加HTML,因此您将看不到任何结果)。
但是您似乎在计算参数方面存在疑问,我认为这些可能是导致问题的根源:
如果该值为负或大于100.000,则您什么也不做
您声明了个月,但是它没有任何价值(您可以在计算中使用它)
未来和开始的值是0(您可以定义),并且这些0会在计算中传递,
现在,未来,开始和经纪人是全球性的(它们不是 var , const 或 let )
使用更加结构化的代码方法,您可以看到这些计算问题。
// calculate interest
function calcInterest(val) {
const interest = 0
// you don't need to specify the lower bount of the "else if"s, because you can be sure that it will hit only one of them
if (0 <= val && val < 5000) {
interest = 0.12
} else if (val <= 10000) {
interest = 0.10
} else if (val <= 20000) {
interest = 0.08
} else if (val <= 35000) {
interest = 0.0725
} else if (val <= 60000) {
interest = 0.0695
} else if (val <= 60000) {
interest = 0.0695
} else if (val <= 100000) {
interest = 0.0675
} // you don't have a "default else", so if val is 100001+, or is a negative number, then interest will be 0
return interest
}
function pmt(rate_per_period, number_of_payments, present_value, future_value, type) {
if (rate_per_period != 0.0) {
// Interest rate exists
var q = Math.pow(1 + rate_per_period, number_of_payments);
return -(rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
} else if (number_of_payments != 0.0) {
// No interest rate, but number of payments exists
return -(future_value + present_value) / number_of_payments;
}
return 0;
}
// this function gets a lot of parameters
// future and beginning are always 0
function calcPayments(interest, months, brokerage, present, future, beginning) {
var payment = -pmt((interest / 12), months, (Number(brokerage) + Number(present)), future, beginning)
var payment_2dp_exgst = payment.toFixed(2);
var payment_2dp_gst = ((payment * 1.1) - payment).toFixed(2);
var payment_2dp_incgst = (Number(payment_2dp_exgst) + Number(payment_2dp_gst)).toFixed(2);
return {
payment_2dp_exgst,
payment_2dp_gst,
payment_2dp_incgst
}
}
// main function
function calcLoanezi(present, term) {
const interest = 60 < term ? calcInterest(present) + 0.01 : calcInterest(present)
let months,
future = 0,
beginning = 0,
brokerage = present * 0.06
return calcPayments(interest, months, brokerage, present, future, beginning)
}
// DOM manipulation
function appendToDOM() {
const {
payment_2dp_exgst,
payment_2dp_gst,
payment_2dp_incgst
} = calcLoanezi(Number($("#invoice").val()), Number($("#term").val()))
jQuery("#monthlyex").val("$" + payment_2dp_exgst);
jQuery("#monthlygst").val("$" + payment_2dp_gst);
jQuery("#monthlyinc").val("$" + payment_2dp_incgst);
}
jQuery(document).ready(function() {
$("#invoice").keyup(function() {
appendToDOM();
});
$("#term").on('change', function() {
appendToDOM();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>