[英]How can I stop an alert in javascript from firing when the page loads, and make it run when a button is clicked?
Without using jQuery, how can I get a JavaScript alert to fire at the right time? 不使用jQuery,如何在适当的时间触发JavaScript警报? It's currently firing when the page loads. 当前在页面加载时触发。 I created a temp converter and would like the "convert to fahr" button and the "convert to cels" button to fire off the converted temps in a pop up "alert". 我创建了一个临时转换器,希望使用“转换为fahr”按钮和“转换为cels”按钮在弹出的“警报”中触发转换后的温度。 Currently both buttons are successfully executing their respective temp conversion math functions but I'd also like to add the alerts when the buttons are clicked. 当前,这两个按钮都可以成功执行各自的温度转换数学函数,但是我也想在单击按钮时添加警报。
document.getElementById('convert').onclick = makeTempF();
document.getElementById('convertC').onclick = makeTempC();
function makeTempF() {
var d, b, c;
var f = document.getElementById('f').value;
b = f - 32;
c = b * 5;
d = parseInt(c / 9);
alert('Celsius Temperature is ' +d);
// console.log(d);
}
function makeTempC() {
var g, b, d;
var c = document.getElementById('c').value;
b = c * 9;
d = b / 5;
g = parseInt(d +32);
alert('Fahrenheit Temperature is ' +g);
// console.log(g);
}
Assign the callbacks, not what their invocation returns. 分配回调,而不是其调用返回的值。
document.getElementById('convert').onclick = makeTempF;
document.getElementById('convertC').onclick = makeTempC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.