我使用谷歌浏览器查看为什么我的测验结束不正常以及为什么结果没有显示在文本框中,并且说没有定义getResults但是对我来说一切都很好。此外还有另一个错误,一直出现,并显示Uncaught SyntaxError:Unexpected token ILLEGAL。这个错误是否因为getResults错误而出现,还是与完全不同的东西有关?请帮助我如此接近完成测验,但这必须发生。顺便说一句,这是代码:
<html>
<body>
<h1></h1>
<form>
<ol>
<li> How much are you willing to spend on a phone per month?</li>
<ul>
<li><input type = "radio" name = "q1" id="q1_1"> £5-£10.</input></li>
<li><input type = "radio" name = "q1" id="q1_2"> £10-£15.</input></li>
<li><input type = "radio" name = "q1" id="q1_3"> £15-£20.</input></li>
<li><input type = "radio" name = "q1" id="q1_4"> £20-£25.</input></li>
<li><input type = "radio" name = "q1" id="q1_5"> £25-£30.</input></li>
<li><input type = "radio" name = "q1" id="q1_6"> £30-£35.</input></li>
<li><input type = "radio" name = "q1" id="q1_7"> £35-£40.</input></li>
</ul>
<li> Are you good with technology</li>
<ul>
<li><input type = "radio" name = "q2" id="q2_1"> Yes.</input></li>
<li><input type = "radio" name = "q2" id="q2_2"> No.</input></li>
</ul>
<li> Are you looking for an easy to use phone</li>
<ul>
<li><input type = "radio" name = "q3" id="q3_1"> Yes.</input></li>
<li><input type = "radio" name = "q3" id="q3_2"> No.</input></li>
</ul>
<li> Are you looking for a modern type of phone?</li>
<ul>
<li><input type = "radio" name = "q4" id="q4_1"> Yes.</input></li>
<li><input type = "radio" name = "q4" id="q4_2"> No.</input></li>
</ul>
<li> How big do you want the phone to be?</li>
<ul>
<li><input type = "radio" name = "q5" id="q5_1"> Big.</input></li>
<li><input type = "radio" name = "q5" id="q5_2"> Medium.</input></li>
<li><input type = "radio" name = "q5" id="q5_3"> Small.</input></li>
<li><input type = "radio" name = "q5" id="q5_4"> I don't really mind.</input></li>
</ul>
<li> Do you care about the colour of the phone?</li>
<ul>
<li><input type = "radio" name = "q6" id="q6_1"> Yes.</input></li>
<li><input type = "radio" name = "q6" id="q6_2"> No.</input></li>
</ul>
<li> Have you ever owned a phone before?</li>
<ul>
<li><input type = "radio" name = "q7" id="q7_1"> Yes.</input></li>
<li><input type = "radio" name = "q7" id="q7_2"> No.</input></li>
</ul>
<li> Do you want to be able to use the phone to get out of awkward social situations?</li>
<ul>
<li><input type = "radio" name = "q8" id="q8_1"> Yes.</input></li>
<li><input type = "radio" name = "q8" id="q8_2"> No.</input></li>
</ul>
<li> Do you want to be able to access the app store and download apps using your phone?</li>
<ul>
<li><input type = "radio" name = "q9" id="q9_1"> Yes.</input></li>
<li><input type = "radio" name = "q9" id="q9_2"> No.</input></li>
</ul>
<li> What happened to the last phone you owned?</li>
<ul>
<li><input type = "radio" name = "q10" id="q10_1"> I got bored of it.</input></li>
<li><input type = "radio" name = "q10" id="q10_2"> It broke.</input></li>
<li><input type = "radio" name = "q10" id="q10_3"> The contract ran out.</input></li>
<li><input type = "radio" name = "q10" id="q10_4"> Other.</input></li>
</ul>
</ol>
<input type = "button" value = "Submit" onclick="getResults()"> <input type = "reset" value = "Clear"></input>
<textarea id="result">The right phone for you will be displayed here.</textarea>
</html>
<script>
function getResults() {
if (document.getElementById('q1_1').checked &&
document.getElementById('q2_1').checked &&
document.getElementById('q3_1').checked &&
document.getElementById('q4_1').checked &&
document.getElementById('q5_1').checked &&
document.getElementById('q6_1').checked &&
document.getElementById('q7_1').checked &&
document.getElementById('q8_1').checked &&
document.getElementById('q9_1').checked &&
document.getElementById('q10_1').checked
) {
document.getElementById('result').innerHTML = 'Unfortunately, the iPhone is the right phone for you.';
}
}
</script>
</body>
答案 0 :(得分:1)
我在Firefox的页面源查看器中查看了页面的源代码:
每个<input>
都是错误的。你有这个:
<input type = "radio" name = "q1" id="q1_1"> £5-£10.</input>
但尾随</input>
是不必要的。你应该这样:
<input type = "radio" name = "q1" id="q1_1"> £5-£10.
另外:你的结束</html>
标记应该是文件中的最后一个标记,但是在脚本之前你已经了。我猜有些浏览器可能会在</html>
您没有结束</form>
代码,该代码应位于<script>
区块之前。
您可能会发现在页面顶部添加<!doctype html>
以及像这样的<head>
块非常有用:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
将对字符编码进行排序,以便£
正确显示。
但是您没有提到您的网页已嵌入其他网站。因此,您的代码中不需要以下代码:<!doctype>
,<html>
,</html>
,<head>
(我建议的整个标题块可以删除), <body>
,</body>
。
您仍然需要插入缺少的结束</form>
标记。
您报告的问题是由于在您的脚本块中将&&
替换为&&
的包含页面。这是脚本块中的语法错误,意味着您的getResults()
函数未被解析。您必须检查网站以了解如何在不发生这种情况的情况下包含脚本。