gpt4 book ai didi

javascript - jquery 函数损坏 - 不更新 onchange

转载 作者:行者123 更新时间:2023-11-27 23:38:25 24 4
gpt4 key购买 nike

该页面是一个示例抵押贷款计算器,其中包含一个表格以显示所选输入的每月还款额。我使用了我之前制作的抵押贷款计算器中的脚本,您可以在其中手动输入利率 <input type="number"> .我现在想从 <td> 中解析利率并计算并输出到同一个<tr>中的每月还款栏。 .

我在某处弄乱了代码,因为它不再有效了吗?

而且它从不更新表单中输入的更改,这可以修复吗?

$(document).ready(function() {
$("#type :checkbox").click(function() {
$("td").parent().hide();
$("#type :checkbox:checked").each(function() {
$("." + $(this).val()).parent().show();
});
});
$("#fee :checkbox").click(function() {
$("td").parent().hide();
$("#fee :checkbox:checked").each(function() {
$("." + $(this).val()).parent().show();
});
});
});

window.onload = function() {
document.repaymentcalc.homevalue.onchange = repayment;
document.repaymentcalc.loanamount.onchange = repayment;
document.repaymentcalc.interestrate.onchange = repayment;
document.repaymentcalc.numberpayments.onchange = repayment;

// populate repayment in table
$('tbody tr').each(function(idx, row) {
var $row = $(row);
var initialRateCell = $row.find('td')[2]; // get column 4 (for the inital rate)
var repaymentCell = $row.find('td').last() // get the output cell (last in the row)
var rate = parseFloat($(initialRateCell).html()); // parse the rate from the rate cell
var repaymentVal = computeRepayment(rate); // compute the new repayment value
console.log(repaymentVal)
$(repaymentCell).html(repaymentVal.repayment); // stick it in the result cell
});
}

function computeRepayment(rate) {
// if the passed in rate is available, use it.
// otherwise pull it from document.repaymentcalc
var rate = rate || document.repaymentcalc.interestrate.value
var x = parseInt(document.repaymentcalc.loanamount.value, 10);
var y = parseInt(rate * 100, 10) / 120000;
var z = parseInt(document.repaymentcalc.numberpayments.value, 10) * 12;
var h = parseInt(document.repaymentcalc.homevalue.value, 10);

var repayment = y * x * Math.pow((1 + y), z) / (Math.pow((1 + y), z) - 1);

var loantovalue = x / h * 100;

var year = z / 12;
return {
repayment: repayment,
loantovalue: loantovalue,
year: year
}
}

function repayment(rate){
var repaymentInfo = computeRepayment(rate);
document.getElementById("repayments").innerHTML = 'Monthly Repayment: £' + repaymentInfo.repayment.toFixed(2);
document.getElementById("ltv").innerHTML = 'Loan to Value: ' + repaymentInfo.loantovalue.toFixed(1) + '%';
document.getElementById("years").innerHTML = repaymentInfo.year + ' years';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="type">
<p id="Mortgage Type">Mortgage Type</p>
<input type="checkbox" name="type" value="t2" id="t2" />2yr Fixed
<br>
<input type="checkbox" name="type" value="t3" id="t3" />3yr Fixed
<br>
<input type="checkbox" name="type" value="t5" id="t5" />5yr Fixed
<br>
<input type="checkbox" name="type" value="t9" id="t9" />Tracker
<br>
<input type="checkbox" name="type" value="t1" id="t1" checked/>All
<br>
</section>

<section id="fee">
<p id="Fee">Fee</p>
<input type="checkbox" name="fee" value="f2" id="f2" />Fee
<br>
<input type="checkbox" name="fee" value="f3" id="f3" />No Fee
<br>
<input type="checkbox" name="fee" value="f1" id="f1" checked/>All
<br>
</section>

<form name="repaymentcalc" action="">
</br>
<p>
Home Value £
<input type="number" id="homevalue" value="250000" style="width: 75px">
</p>

<p>
Loan Amount £
<input type="number" id="loanamount" value="200000" style="width: 75px">
</p>

Term
<input type="range" id="numberpayments" value="25" min="1" max="40" style="width: 100px">
<div id="years" style="display:inline-block;">25 years</div>

<p>
<div id="ltv">Loan to Value: 80.0%</div>

</div>
</form>

<br>

<div id="mortgagediv">
<table id="mortgagetable">
<thead>
<tr class="productheader">
<th class="lender">Lender</th>
<th class="type">Type</th>
<th class="inititalrate">Initial Rate (%)</th>
<th class="svr">SVR (%)</th>
<th class="apr">Overall APR (%)</th>
<th class="fee">Fee (£)</th>
<th class="ltv">LTV (%)</th>
<th class="repayment">Monthly Repayment (£)</th>
</tr>
</thead>
<tbody>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t2">2yr Fixed</td>
<td class="tg-031e">1.64</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f3"></td>
<td class="tg-031e">70</td>
<td class="tg-031e"></td>
</tr>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t3">3yr Fixed</td>
<td class="tg-031e">1.69</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f3"></td>
<td class="tg-031e">75</td>
<td class="tg-031e"></td>
</tr>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t5">5yr Fixed</td>
<td class="tg-031e">1.79</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f3"></td>
<td class="tg-031e">80</td>
<td class="tg-031e"></td>
</tr>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t9">Tracker</td>
<td class="tg-031e">1.64</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f3"></td>
<td class="tg-031e">70</td>
<td class="tg-031e"></td>
</tr>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t2">2yr Fixed</td>
<td class="tg-031e">1.69</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f2">999</td>
<td class="tg-031e">75</td>
<td class="tg-031e"></td>
</tr>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t3">3yr Fixed</td>
<td class="tg-031e">1.79</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f2">999</td>
<td class="tg-031e">80</td>
<td class="tg-031e"></td>
</tr>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t5">5yr Fixed</td>
<td class="tg-031e">1.79</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f2">999</td>
<td class="tg-031e">80</td>
<td class="tg-031e"></td>
</tr>
<tr class="product">
<td class="tg-031e">Nationwide</td>
<td class="t1 t9">Tracker</td>
<td class="tg-031e">1.79</td>
<td class="tg-031e">3.99</td>
<td class="tg-031e">3.40</td>
<td class="f1 f2">999</td>
<td class="tg-031e">80</td>
<td class="tg-031e"></td>
</tr>
</tbody>
</table>
</div>

最佳答案

如果你在你的内部添加一个调试器

window.onload = function() {

你会注意到你永远不会进入函数内部

尝试将该功能放入 document.ready

此外,您的 onchange 赋值无效,我已将其设为 jquery 选择器

http://jsfiddle.net/eood7x1b/2/

$(document).ready(function() {
$("#type :checkbox").click(function() {
$("td").parent().hide();
$("#type :checkbox:checked").each(function() {
$("." + $(this).val()).parent().show();
});
});
$("#fee :checkbox").click(function() {
$("td").parent().hide();
$("#fee :checkbox:checked").each(function() {
$("." + $(this).val()).parent().show();
});
});
$(document.repaymentcalc.homevalue).onchange = repayment;
$(document.repaymentcalc.loanamount).onchange = repayment;
$(document.repaymentcalc.interestrate).onchange = repayment;
$(document.repaymentcalc.numberpayments).onchange = repayment;
// populate repayment in table
$('tbody tr').each(function(idx, row) {
var $row = $(row);
var initialRateCell = $row.find('td')[2]; // get column 4 (for the inital rate)
var repaymentCell = $row.find('td').last() // get the output cell (last in the row)
var rate = parseFloat($(initialRateCell).html()); // parse the rate from the rate cell
var repaymentVal = computeRepayment(rate); // compute the new repayment value
console.log(repaymentVal)
$(repaymentCell).html(repaymentVal.repayment); // stick it in the result cell
});
});

function computeRepayment(rate) {
// if the passed in rate is available, use it.
// otherwise pull it from document.repaymentcalc
var rate = rate || document.repaymentcalc.interestrate.value
var x = parseInt(document.repaymentcalc.loanamount.value, 10);
var y = parseInt(rate * 100, 10) / 120000;
var z = parseInt(document.repaymentcalc.numberpayments.value, 10) * 12;
var h = parseInt(document.repaymentcalc.homevalue.value, 10);

var repayment = y * x * Math.pow((1 + y), z) / (Math.pow((1 + y), z) - 1);

var loantovalue = x / h * 100;

var year = z / 12;
return {
repayment: repayment,
loantovalue: loantovalue,
year: year
}
}

function repayment(rate){
var repaymentInfo = computeRepayment(rate);
document.getElementById("repayments").innerHTML = 'Monthly Repayment: £' + repaymentInfo.repayment.toFixed(2);
document.getElementById("ltv").innerHTML = 'Loan to Value: ' + repaymentInfo.loantovalue.toFixed(1) + '%';
document.getElementById("years").innerHTML = repaymentInfo.year + ' years';
}

关于javascript - jquery 函数损坏 - 不更新 onchange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com