附加的javascript函數應該使用兩個輸入範圍中的值以及表中初始單元格的值。然後它應該計算貸款的總還款額。Javascript計算函數未返回值
這是不是在預計'td還款'單元輸出?
<script>
var repayment = function() {
};
document.repaymentcalc.amount.onchange = repayment;
};
makeSomething();
function makeSomething() {
$('tbody tr').each(function(idx, row) {
var $row = $(row);
var initialRateCell = $row.find('td')[1];
var repaymentCell = $row.find('td')[3];
var rate = parseFloat($(initialRateCell).html());
var repaymentVal = computeRepayment(rate);
$(repaymentCell).html(repaymentVal.repayment);
});
}
$("#calcform :input").change(function() {
makeSomething();
});
function computeRepayment(rate) {
var x = parseInt(document.repaymentcalc.amount.value, 10);
var y = parseInt(rate, 10)/36500;
var z = parseInt(document.repaymentcalc.numberpayments.value, 10);
var repayment = (x * y * z) + x;
return {
repayment: '£' + repayment.toFixed(2),
};
}
< /script>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form name="repaymentcalc" id="calcform" action="">
Amount
<input id="amount" type="range" min="0" max="2500" step="50" value="100" style="width: 150px">Term
<input id="numberpayments" type="range" min="0" max="420" step="30" value="30" style="width: 150px">
</form>
<table id="mortgagetable">
<thead>
<tr class="producthd">
<th class="lenderhd">Lender</th>
<th class="initialratehd">% p.a. fixed</th>
<th class="rateshd">% APR</th>
<th class="repaymenthd">Total Repayment</th>
</tr>
</thead>
<tbody>
<tr class="product">
<td class="lender">Bank1</td>
<td class="initialrate">285</td>
<td class="rates">1204.39</td>
<td class="repayment"></td>
</tr>
<tr class="product">
<td class="lender">Bank2</td>
<td class="initialrate">292</td>
<td class="rates">1221.5</td>
<td class="repayment"></td>
</tr>
<tr class="product">
<td class="lender">Bank3</td>
<td class="initialrate">292</td>
<td class="rates">1295</td>
<td class="repayment"></td>
</tr>
<tr class="product">
<td class="lender">Bank4</td>
<td class="initialrate">128</td>
<td class="rates">1221.5</td>
<td class="repayment"></td>
</tr>
</tbody>
</table>
要從一個函數返回一個值,你需要一個'return'語句。 – Pointy
首先,你的還款功能是空的。所以當然沒有什麼會發生。 其次,函數computePayment中的本地可變還款是本地的,所以它在通話結束時下降。 –
另外,您發佈的代碼在語法上不是正確的,所以在解決該問題之前它不會執行任何操作。檢查開發者控制檯。 – Pointy