js函数不返回onclick
js function not returning onclick
我创建了一个无法使用 onclick return 访问的函数。
我确定这是我所缺少的简单东西。
HTML:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="schedule.js"></script>
</head>
<body>
<input type="button" onclick="computeSchedule();" value="Submit" /><br>
<p id="scheduleOutput"></p>
</body>
</html>
JS:
function computeSchedule(loan_amount, interest_rate, payments_per_year, years, payment) {
var schedule = [];
var remaining = loan_amount;
var number_of_payments = payments_per_year * years;
for (var i=0; i<=number_of_payments; i++) {
var interest = remaining * (interest_rate/100/payments_per_year);
var principle = (payment-interest);
var row = [i, principle>0?principle:0, interest>0?interest:0, remaining>0?remaining:0];
schedule.push(row);
remaining -= principle
}
return schedule;
}
var list = JSON.stringify(computeSchedule(100000, 0.005, 12, 15, 843.86), 0, 4)
document.getElementById('scheduleOutput').innerHTML = list;
我可以在 jsFiddle onload, but when I change js settings to "no wrap - in head" 中自动获取 return 的功能,但它不起作用。当我尝试在 jsFiddle 之外 运行?
时,我假设正在发生的事情也会发生
当您单击按钮时,函数 computeSchedule() 将被触发,但没有任何值被传递给它。我敢打赌,如果您将值放在 html 行的参数中,它就会起作用。
<input type="button" onclick="computeSchedule(100000, 0.005, 12, 15, 843.86);" value="Submit" />
您在正文内容准备就绪之前就已经编写了 运行 秒。这就是为什么 document.getElementById
似乎不起作用,因为此时不存在这样的元素。
尝试将您的代码包装在简单的 "content load" 侦听器中,以确保在所有元素准备就绪后它将 运行:
document.addEventListener('DOMContentLoaded', function() {
function computeSchedule(loan_amount, interest_rate, payments_per_year, years, payment) {
var schedule = [];
var remaining = loan_amount;
var number_of_payments = payments_per_year * years;
for (var i=0; i<=number_of_payments; i++) {
var interest = remaining * (interest_rate/100/payments_per_year);
var principle = (payment-interest);
var row = [i, principle>0?principle:0, interest>0?interest:0, remaining>0?remaining:0];
schedule.push(row);
remaining -= principle
}
return schedule;
}
var list = JSON.stringify(computeSchedule(100000, 0.005, 12, 15, 843.86), 0, 4)
document.getElementById('scheduleOutput').innerHTML = list;
});
是的,您的 onclick 现在没有向该函数传递任何数据。因此,您还需要将参数传递给它,一切都应该正常工作。
我创建了一个无法使用 onclick return 访问的函数。
我确定这是我所缺少的简单东西。
HTML:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="schedule.js"></script>
</head>
<body>
<input type="button" onclick="computeSchedule();" value="Submit" /><br>
<p id="scheduleOutput"></p>
</body>
</html>
JS:
function computeSchedule(loan_amount, interest_rate, payments_per_year, years, payment) {
var schedule = [];
var remaining = loan_amount;
var number_of_payments = payments_per_year * years;
for (var i=0; i<=number_of_payments; i++) {
var interest = remaining * (interest_rate/100/payments_per_year);
var principle = (payment-interest);
var row = [i, principle>0?principle:0, interest>0?interest:0, remaining>0?remaining:0];
schedule.push(row);
remaining -= principle
}
return schedule;
}
var list = JSON.stringify(computeSchedule(100000, 0.005, 12, 15, 843.86), 0, 4)
document.getElementById('scheduleOutput').innerHTML = list;
我可以在 jsFiddle onload, but when I change js settings to "no wrap - in head" 中自动获取 return 的功能,但它不起作用。当我尝试在 jsFiddle 之外 运行?
时,我假设正在发生的事情也会发生当您单击按钮时,函数 computeSchedule() 将被触发,但没有任何值被传递给它。我敢打赌,如果您将值放在 html 行的参数中,它就会起作用。
<input type="button" onclick="computeSchedule(100000, 0.005, 12, 15, 843.86);" value="Submit" />
您在正文内容准备就绪之前就已经编写了 运行 秒。这就是为什么 document.getElementById
似乎不起作用,因为此时不存在这样的元素。
尝试将您的代码包装在简单的 "content load" 侦听器中,以确保在所有元素准备就绪后它将 运行:
document.addEventListener('DOMContentLoaded', function() {
function computeSchedule(loan_amount, interest_rate, payments_per_year, years, payment) {
var schedule = [];
var remaining = loan_amount;
var number_of_payments = payments_per_year * years;
for (var i=0; i<=number_of_payments; i++) {
var interest = remaining * (interest_rate/100/payments_per_year);
var principle = (payment-interest);
var row = [i, principle>0?principle:0, interest>0?interest:0, remaining>0?remaining:0];
schedule.push(row);
remaining -= principle
}
return schedule;
}
var list = JSON.stringify(computeSchedule(100000, 0.005, 12, 15, 843.86), 0, 4)
document.getElementById('scheduleOutput').innerHTML = list;
});
是的,您的 onclick 现在没有向该函数传递任何数据。因此,您还需要将参数传递给它,一切都应该正常工作。