Javascript getResults未定义错误
我用Google chrome查看了为什么我的测验结束时没有正常工作,为什么结果没有显示在文本框中,它说getResults没有定义,但在我看来一切正常。还有另一个错误不断出现,它表示未捕获的SyntaxError:意外标记非法。这个错误是因为getResults错误而出现的,还是与完全不同的事情有关?请帮帮我,我马上就要完成测验了,但这不得不发生。顺便说一下,下面是代码:Javascript getResults未定义错误,javascript,html,Javascript,Html,我用Google chrome查看了为什么我的测验结束时没有正常工作,为什么结果没有显示在文本框中,它说getResults没有定义,但在我看来一切正常。还有另一个错误不断出现,它表示未捕获的SyntaxError:意外标记非法。这个错误是因为getResults错误而出现的,还是与完全不同的事情有关?请帮帮我,我马上就要完成测验了,但这不得不发生。顺便说一下,下面是代码: <html> <body> <h1></h1> <form>
<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>
你每月愿意花多少钱买一部手机
- 5-10英镑。
- 10-15英镑
- 15-20英镑
- 20-25英镑
- 25-30英镑。
- 30-35英镑
- 35-40英镑
你擅长科技吗
- 对
- 没有
你在寻找一款易于使用的手机吗
- 对
- 没有
你在找一款现代手机吗
- 对
- 没有
你希望手机有多大
- 大
- 中等
- 小的
- 我真的不介意
你在乎电话的颜色吗
- 对
- 没有
你以前有过手机吗
- 对
- 没有
你想用手机摆脱尴尬的社交场合吗
- 对
- 没有
您想使用手机访问应用商店并下载应用吗
- 对
- 没有
你最后一部手机怎么了
- 我厌倦了
- 它坏了
- 合同到期了
- 其他的
此处将显示适合您的手机。
函数getResults(){
如果(document.getElementById('q1_1')。已选中&&
document.getElementById('q2_1')。已选中&&
document.getElementById('q3_1')。已选中&&
document.getElementById('q4_1')。已选中&&
document.getElementById('q5_1')。已选中&&
document.getElementById('q6_1')。已选中&&
document.getElementById('q7_1')。已选中&&
document.getElementById('q8_1')。已选中&&
document.getElementById('q9_1')。已选中&&
文档.getElementById('q10_1')。已选中
) {
document.getElementById('result').innerHTML='不幸的是,iPhone正是适合你的手机';
}
}
我在Firefox的页面源代码查看器中查看了您页面的源代码:
每个
都是错误的。你有这个:
<input type = "radio" name = "q1" id="q1_1"> £5-£10.</input>
<input type = "radio" name = "q1" id="q1_1"> £5-£10.
5-10英镑。
但是后面的
是不必要的。你应该有这个:
<input type = "radio" name = "q1" id="q1_1"> £5-£10.</input>
<input type = "radio" name = "q1" id="q1_1"> £5-£10.
5-10英镑。
另外:结束标记应该是文件中的最后一项,但它在脚本之前。我猜一些浏览器可能会忽略
您没有结束标记
,它应该正好位于
块的前面
您可能会发现在页面顶部添加一个
和一个
块很有帮助,如下所示:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
这将对字符编码进行排序,以便您的
正确显示
但是您没有提到您的页面正在嵌入另一个站点。因此,代码中不需要以下标记:
,
,
,
,
(可以删除我建议的整个标题块),
,
您仍然需要插入缺少的结束标记
您报告的问题是由于包含页面将
&&
替换为&&代码>在脚本块中。这是脚本块中的语法错误,表示未解析getResults()
函数。您必须查看该站点,了解如何在不发生这种情况的情况下包含脚本。对我来说在FF24和Chrome 30中都有效,只要每个问题都选择了第一个选项。如果没有,代码也不会改变结果框。它似乎在除我的站点之外的任何地方都有效。@Keon因为我无法在测试服务器上复制该问题,你能在网上的某个地方发布一个链接,在那里可以找到有问题的页面吗?我来看看。@Keon你没有提到你将测验嵌入到另一个网站的页面中。我已经用更多信息更新了我的答案。