Javascript-我可以'我没有发现我的错误.(单选按钮)
Javascript - I can't find my mistake.(radio buttons)
我想创建一个在线测试,但我的第一个问题有问题。下面的代码不起作用,即使检查了正确的答案,也会返回"你的分数为0"。
<!DOCTYPE html>
<html>sssdsdsdsd
<head>Mypage.com
<title> myPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script>
var total=0;
var yourmark="your score is ";
if(document.getElementById('q12').checked) {
total=total+1;
}else {
total=total;
}
</script>
<script>
function showScore() {
alert(yourmark + total );
}
</script>
</br>
<input type="radio" name="question1" id="q11" value="false">Question 1-first option <br>
<input type="radio" name="question1" id="q12" value="true" > Question 1- second option<br>
<input type="radio" name="question1" value="false">Question 1- third option<br>
<button onclick="showScore()">Show my score</button>
</body>
</html>
列出了事情没有按您的意愿运行的直接原因。
-
在分析和呈现元素之前,您可以设置总和的值。HTML将从上到下进行解析。因此,您希望将脚本移到底部。
-
如果需要在标记中设置选项框的值,请使用checked而不是value属性。
你应该有类似下面的片段
<!DOCTYPE html>
<html>sssdsdsdsd
<head>Mypage.com
<title> myPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script>
function showScore() {
alert(yourmark + total );
}
</script>
</br>
<input type="radio" name="question1" id="q11">Question 1-first option <br>
<input type="radio" name="question1" id="q12" checked="true" > Question 1- second option<br>
<input type="radio" name="question1">Question 1- third option<br>
<button onclick="showScore()">Show my score</button>
<script>
var total=0;
var yourmark="your score is ";
if(document.getElementById('q12').checked) {
total=total+1;
}else {
total=total;
}
</script>
</body>
</html>
http://codepen.io/anon/pen/vgJuA
因此,将var total=0作为函数外的全局变量,并创建一个函数checkAnswer()
。您的javascript部分现在将是:
var total = 0;
function checkAnswer() {
//total is the global Variable for the score
if (document.getElementById('q12').checked) {
total = total + 1;
} else {
total = total;
}
return total;
}
function showScore() {
var yourmark = "your score is ";
alert(yourmark + checkAnswer());
}
希望它能有所帮助!
此块:
var total=0;
var yourmark="your score is ";
if(document.getElementById('q12').checked) {
total=total+1;
}else {
total=total;
}
在加载时在页面的开头运行。在这一点上total
实际上是0
。当您单击该按钮时,您所要求的只是更改选项后的"总计. At no point are you updating the value of
总计"值。
正如PM 77-1所建议的,最简单的解决方案是将计算移动到函数内部,以便每次需要更新答案时都会计算total
。
<!DOCTYPE html>
<html>sssdsdsdsd
<head>Mypage.com
<title> myPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script>
function showScore() {
var total=0;
var yourmark="your score is ";
if(document.getElementById('q12').checked) {
total=total+1;
}else {
total=total;
}
alert(yourmark + total );
}
</script>
</br>
<input type="radio" name="question1" id="q11" value="false">Question 1-first option <br>
<input type="radio" name="question1" id="q12" value="true" > Question 1- second option<br>
<input type="radio" name="question1" value="false">Question 1- third option<br>
<button onclick="showScore()">Show my score</button>
</body>
</html>
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 我需要一个jQuery函数来只工作在700px以上的屏幕大小,无法在我的代码中发现错误
- 我的代码很完美,但我现在不知道为什么我发现了问题
- 有人能发现我的错误吗(自动完成文本框)
- 在我的javascript文件中发现的这个奇怪的代码是什么
- 我刚刚发现了基于时间的背景更改,但无法重复
- 地理位置发现我的位置不起作用
- 使用javascript,我如何发现打开并运行我的应用程序的软件
- 在我的footer.php中发现了带有html链接的未知javascript
- Javascript-我可以'我没有发现我的错误.(单选按钮)
- 为什么我的javascript函数发现我的复选框为false
- 可以'我在我的小JS代码中没有发现一个错误(用于循环和里面的一个函数)
- 我的外置硬盘感染了病毒,我发现了这个非常可疑的javascript文件.谁能帮我分析一下这份文件
- 我如何发现我的Open Graph应用程序是否已经以编程方式添加到时间轴中?
- 如何发现我的场景是否被加载
- 我如何发现我在以前的流星应用程序中安装了什么包?
- 有人能发现问题与我的jQuery代码- fanybox
- 当尝试为我的电子应用程序创建安装程序时,构建未发现错误
- 从url中提取img-Script在jsfiddle中有效,但在我的网站上无效,任何人都能发现原因
- 我可以发现用户的web浏览器是否为我的自签名ssl证书存储了一个例外吗?