[英]HTML onclick button not calling javascript function
My HTML: 我的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="scorecard.js"></script>
</head>
<body>
<div id="scoreButton"><button onclick="javascript: scorecard();">Calculate Score</button></div>
</body>
</html>
My Javascript: 我的Javascript:
var a;
var b;
var c;
total;
var scorecard = function()
{
a = prompt("What was your score on a?","A");
b = prompt("What was your score on b?","B");
c = prompt("What was your score on c?","C");
total = (a + b + c);
document.getElementById("A").innerHTML = a;
document.getElementById("B").innerHTML = b;
document.getElementById("C").innerHTML = c;
document.getElementById("Total").innerHTML = total;
};
What am I missing? 我想念什么? Everything validates yet I can't get it to run in any browser.
一切都经过验证,但我无法使其在任何浏览器中运行。 I'm sure it must be something obvious, however, I have looked at this code and played with it all day and I'm just lost and very frustrated.
我确定它一定很明显,但是,我整天都在看这段代码并一直使用它,我迷路了,非常沮丧。 Any help would be great.
任何帮助都会很棒。
Multiple issues with your code 您的代码存在多个问题
var total
instead of total
var total
而不是total
Demo 演示版
var a; var b; var c; var total; var scorecard = function() { a = +prompt("What was your score on a?", "A"); b = +prompt("What was your score on b?", "B"); c = +prompt("What was your score on c?", "C"); total = (a + b + c); document.getElementById("A").innerHTML = a; document.getElementById("B").innerHTML = b; document.getElementById("C").innerHTML = c; document.getElementById("Total").innerHTML = total; };
A : <div id="A"></div> <br> B : <div id="B"></div> <br> C : <div id="C"></div> <br> Total : <div id="Total"></div> <br> <div id="scoreButton"><button onclick="javascript: scorecard();">Calculate Score</button></div>
Because variable total
is undefined
. 因为变量
total
是undefined
。 Make sure your total var is something like var total
And in addition to that 确保您的总var类似于
var total
,除此之外
elements with Id's A,B,C,Total
somewhere in document , As you are doing some operation on DOM
which isn't in DOM
. elements with Id's A,B,C,Total
在文档的某个地方,当你正在做一些操作DOM
这是不是在DOM
。 parseInt()
if u want to add numbers otherwise it was ok. parseInt()
如果您想添加数字,否则还可以。 Hope this will work. var a; var b; var c; var total; var scorecard = function() { a = parseInt(prompt("What was your score on a?","A"));//parseInt() if u want to add numbers otherwise it was ok. b = parseInt(prompt("What was your score on b?","B")); c = parseInt(prompt("What was your score on c?","C")); total = (a + b + c); document.getElementById("A").innerHTML = a; //There should be elements with Id's A,B,C,Total somewhere in document. document.getElementById("B").innerHTML = b; document.getElementById("C").innerHTML = c; document.getElementById("Total").innerHTML = total; };
<div id="scoreButton"><button onclick="javascript: scorecard();">Calculate Score</button></div> <div id = "A"></div> <div id = "B"></div> <div id = "C"></div> <div id = "Total"></div>
干得好:
<div id="scoreButton"><button onclick="scorecard()">Calculate Score</button></div> // I have changed syntax of onclick.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
var a;
var b;
var c;
var total;
function scorecard()
{
a = prompt("What was your score on a?", "A");
b = prompt("What was your score on b?", "B");
c = prompt("What was your score on c?", "C");
total = (a + b + c);
document.getElementById("A").innerHTML = a;
document.getElementById("B").innerHTML = b;
document.getElementById("C").innerHTML = c;
document.getElementById("Total").innerHTML = total;
};
</script>
</head>
<body>
<div id="scoreButton"><button onclick="scorecard()">Calculate Score</button></div>
<h3 id="A"></h3>
<h3 id="B"></h3>
<h3 id="C"></h3>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.