如果用户在字段中输入正确的答案,我正在尝试获得全部答案。但它总是返回0不知道为什么。你能帮我解决一下我的错吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Match It</title>
</head>
<body>
<h1>Set Verbs in correct form</h1>
<p>
<table>
<tr>
<th scope="col">Match 1</th>
<th scope ="col">Match 2</th>
</tr>
<tr>
<td>one</td>
<td>two</td>
<td><input type="text" id="answer1" value=""></td>
<td>four</td>
</tr>
<tr>
<td>eleven</td>
<td><input type="text" id="answer2" value=""></td>
<td>thirteen</td>
<td>fourteen</td>
</tr>
</table>
<button onclick="readit()">Check Answer!</button>
<p id="p2"></p>
</p>
Hint: twelve or three
<script type="text/javascript">
//array
var verblist =[];
verblist[0] = ["one", "two", "three", "four"];
verblist[1] = ["eleven", "twelve", "thirteen", "fourteen"];
var verdi = verblist[0][0];
function readit() {
var verb1 = document.getElementById("answer1").value;
var verb2 = document.getElementById("answer2").value;
//control input
var CorrectAnswer = "0";
if(verb1.toUpperCase() == verdi[0][2])
{
CorrectAnswer++;
}
if(verb2.toUpperCase() == verblist[1][1])
{
CorrectAnswer++;
}
document.getElementById("p2").innerHTML = ("You given total " + CorrectAnswer + " Correct Answers.");
}
</script>
</body>
</html>
请检查并帮助我解决它,我尝试了不同的方法,但对我来说它总是显示0作为回报。
答案 0 :(得分:0)
您的代码中似乎存在许多问题。
例如,var verdi = verblist[0][0];
verdi
的值将为“1”,但您尝试在行中对其进行索引:if(verb1.toUpperCase() == verdi[0][2])
此外,此行if(verb2.toUpperCase() == verblist[1][1])
会导致问题。
因为,输入框中输入的任何内容的大写值永远不会与verblist[1][1]
这些是您可以首先开始解决的问题。
答案 1 :(得分:0)
toUpperCase应该是toLowerCase而你的verdi变量不是数组
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Match It</title>
</head>
<body>
<h1>Set Verbs in correct form</h1>
<p>
<table>
<tr>
<th scope="col">Match 1</th>
<th scope ="col">Match 2</th>
</tr>
<tr>
<td>one</td>
<td>two</td>
<td><input type="text" id="answer1" value=""></td>
<td>four</td>
</tr>
<tr>
<td>eleven</td>
<td><input type="text" id="answer2" value=""></td>
<td>thirteen</td>
<td>fourteen</td>
</tr>
</table>
<button onclick="readit()">Check Answer!</button>
<p id="p2"></p>
</p>
Hint: twelve or three
<script type="text/javascript">
//array
var verblist =[];
verblist[0] = ["one", "two", "three", "four"];
verblist[1] = ["eleven", "twelve", "thirteen", "fourteen"];
function readit() {
var verb1 = document.getElementById("answer1").value;
var verb2 = document.getElementById("answer2").value;
//control input
var CorrectAnswer = "0";
if(verb1.toLowerCase() == verblist[0][2]){
CorrectAnswer++;
}
if(verb2.toLowerCase() == verblist[1][1]){
CorrectAnswer++;
}
document.getElementById("p2").innerHTML = ("You given total " + CorrectAnswer + " Correct Answers.");
}
</script>
</body>
</html>