Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';我不明白为什么我的基于分数的测验赢了;行不通_Javascript_Html - Fatal编程技术网

Javascript Can';我不明白为什么我的基于分数的测验赢了;行不通

Javascript Can';我不明白为什么我的基于分数的测验赢了;行不通,javascript,html,Javascript,Html,我想做一个测验。 每个答案都有多个具有不同值的单选按钮。 当用户单击按钮时,“computeForm()”函数应将选中单选按钮的所有值相加,然后将浏览器重定向到正确的网页 此时,单击按钮时不会发生任何事情 html: 在此列表中,我最喜欢的电视节目是: 辛普森一家 大爆炸理论 火影忍者 斯巴达克斯 权力游戏 箭头 行尸走肉 疯子 纪录片 选择“图形小说”的同义词 漫画书 汤姆 报告 有趣的页面 神奇故事 冒险 情景 书籍 列表中我最喜欢的演员: 以利亚·伍德 克里斯蒂安·贝尔 丹尼·德维托

我想做一个测验。 每个答案都有多个具有不同值的单选按钮。 当用户单击按钮时,“computeForm()”函数应将选中单选按钮的所有值相加,然后将浏览器重定向到正确的网页

此时,单击按钮时不会发生任何事情

html:


在此列表中,我最喜欢的电视节目是:

辛普森一家
大爆炸理论
火影忍者
斯巴达克斯
权力游戏
箭头
行尸走肉
疯子
纪录片

选择“图形小说”的同义词

漫画书
汤姆
报告
有趣的页面
神奇故事
冒险
情景
书籍

列表中我最喜欢的演员:

以利亚·伍德
克里斯蒂安·贝尔
丹尼·德维托
竹内俊子
诺曼·里德斯
丹尼尔·戴刘易斯
荷马·辛普森
拉塞尔·克罗

对我来说,一个好的对手是:

笑料
黑暗的生物
超级恶棍
有趣的敌人
个人敌人
由主角的行为定义
真人
老朋友
亡灵

我最喜欢的绘画风格:

现代的
现实的
详细的
漫画
漫画家
黑暗的
美国人

我最喜欢的视觉艺术家:

哑光呻吟
东山昭
杰克·柯比
查理·阿德拉德
手冢
乔·萨科
格泽戈尔茨·罗辛斯基
汉斯·鲁道夫·吉格
没有

从这个列表中,我最喜欢的图形小说是:

斯派克公司;苏西
游戏结束
交叉
毛斯
死亡笔记
三百
Cninkel酒店
蝙蝠侠:黑暗骑士
跨大都会


脚本:

<script type="text/javascript" language="javascript">

function getValue(name){
var s =0;
var o = document.getElementsByName(name);
for (var n = 0; n < o.length; n++){
    if (o.item(n).checked){
        s = parseInt(o.item(n).value);
    }
}

return s;
}


function computeForm(form) {
var total;

var answer1 = parseInt(getValue('answer1'));
var answer2 = parseInt(getValue('answer2'));
var answer3 = parseInt(getValue('answer3'));
var answer4 = parseInt(getValue('answer4'));
var answer5 = parseInt(getValue('answer5'));
var answer6 = parseInt(getValue('answer6'));
var answer7 = parseInt(getValue('answer7'));

total = (answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7);

if (total=0){ window.location="http://www.cartoons-cinema.be/" }
else if (total >0 && total<=7){ window.location="http://en.wikipedia.org/wiki/Sitcom" }
else if (total >7 && total<=14){ window.location="http://en.wikipedia.org/wiki/Anime" }
else if (total >14 && total<=21){ window.location="http://www.asadventure.com/benl/index.cfm" }
else if (total >28 && total<=35){ window.location="https://en.wikipedia.org/wiki/Fantasy" }
else if (total >35 && total<=42){ window.location="http://www.urbandictionary.com/define.php?term=supes" }
else if (total >42 && total<=49){ window.location="http://www.goodreads.com/shelf/show/post-apocalyptic?auto_login_attempted=true" }
else if (total > 49){ window.location="https://en.wikipedia.org/wiki/Non-fiction" }
}
</script>

函数getValue(名称){
var s=0;
var o=document.getElementsByName(名称);
对于(变量n=0;n0&&total7&&total14&&total28&&total35&&total42&&Total49){window.location=”https://en.wikipedia.org/wiki/Non-fiction" }
}
这是一份为您工作的工作清单

您的初始if缺少一个“=”


您是否检查了控制台的错误?
(var n=0;n
如何拼写
length
?但是代码中可能也存在其他问题。我没有发现错误。我使用
console.log(o.item(n).value)检查了函数getValue(name);
返回值,因此我认为问题在于if-else结构。
if(total=0)
应该是
if(total==0)
(它将
total
设置为零,这是错误的,因此条件是错误的,因此所有其他条件也是错误的。)调试javascript有几种方法。Firebug只是其中一种方法。谢谢,我只是一个新手,但我应该看到这一种……这就是为什么我要为您安装一个提琴。提琴中还有一些额外的清理项目,所以您可能要复制粘贴它。检查类似的东西的方法也是控制台。log()在本例中是if/else后面的“total”值。因为您知道它显然没有触发,这样做将允许您在它命中该语句后检查total值。我错过了JSFIDLE的第一次通读,但是哇,这很有用!谢谢!我想我会经常使用它。
<script type="text/javascript" language="javascript">

function getValue(name){
var s =0;
var o = document.getElementsByName(name);
for (var n = 0; n < o.length; n++){
    if (o.item(n).checked){
        s = parseInt(o.item(n).value);
    }
}

return s;
}


function computeForm(form) {
var total;

var answer1 = parseInt(getValue('answer1'));
var answer2 = parseInt(getValue('answer2'));
var answer3 = parseInt(getValue('answer3'));
var answer4 = parseInt(getValue('answer4'));
var answer5 = parseInt(getValue('answer5'));
var answer6 = parseInt(getValue('answer6'));
var answer7 = parseInt(getValue('answer7'));

total = (answer1 + answer2 + answer3 + answer4 + answer5 + answer6 + answer7);

if (total=0){ window.location="http://www.cartoons-cinema.be/" }
else if (total >0 && total<=7){ window.location="http://en.wikipedia.org/wiki/Sitcom" }
else if (total >7 && total<=14){ window.location="http://en.wikipedia.org/wiki/Anime" }
else if (total >14 && total<=21){ window.location="http://www.asadventure.com/benl/index.cfm" }
else if (total >28 && total<=35){ window.location="https://en.wikipedia.org/wiki/Fantasy" }
else if (total >35 && total<=42){ window.location="http://www.urbandictionary.com/define.php?term=supes" }
else if (total >42 && total<=49){ window.location="http://www.goodreads.com/shelf/show/post-apocalyptic?auto_login_attempted=true" }
else if (total > 49){ window.location="https://en.wikipedia.org/wiki/Non-fiction" }
}
</script>
if(total==0)