Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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 使用单选按钮的输入计算分数_Javascript_Html - Fatal编程技术网

Javascript 使用单选按钮的输入计算分数

Javascript 使用单选按钮的输入计算分数,javascript,html,Javascript,Html,我想使用函数check()并通过它传递单选按钮的名称,以便注册所有按钮的输入。但似乎我无法使用onclick html属性传递任何内容。我打算添加更多的问题,因此我需要一种方法来轻松地为所有问题使用check()函数 <div class="questionCard" id="q1"> <p>Each problem consists of three statements. Based on the first two statement

我想使用函数check()并通过它传递单选按钮的名称,以便注册所有按钮的输入。但似乎我无法使用onclick html属性传递任何内容。我打算添加更多的问题,因此我需要一种方法来轻松地为所有问题使用check()函数

<div class="questionCard" id="q1">
                <p>Each problem consists of three statements. Based on the first two statements, the third statement may be true, false, or uncertain.</br></br>
                1.</br>
                Tanya is older than Eric.</br>
                Cliff is older than Tanya.</br>
                Eric is older than Cliff.</br>
                If the first two statements are true, the third statement is</p>
                <input type="radio" name="a1" onclick="check(a1)" value="true"/>True
                <input type="radio" name="a1" onclick="check(a1)" value="false"/>False
                <input type="radio" name="a1" onclick="check(a1)" value="uncertain"/>Uncertain
            </div>
            <div class="questionCard" id="q2">
                <p>
                2.</br>
                Blueberries cost more than strawberries.</br>
                Blueberries cost less than raspberries.</br>
                Raspberries cost more than strawberries and blueberries.</br>
                If the first two statements are true, the third statement is</p>
                <input type="radio" name="a2" value="true"/>True
                <input type="radio" name="a2" value="false"/>False
                <input type="radio" name="a2" value="uncertain"/>Uncertain
            </div>
            <div class="questionCard" id="q3">
                <p>
                3.</br>
                All the trees in the park are flowering trees.</br>
                Some of the trees in the park are dogwoods.</br>
                All dogwoods in the park are flowering trees.</br>
                If the first two statements are true, the third statement is</p>
                <input type="radio" name="a3" value="true"/>True
                <input type="radio" name="a3" value="false"/>False
                <input type="radio" name="a3" value="uncertain"/>Uncertain
            </div>

每个问题由三个陈述组成。根据前两个陈述,第三个陈述可能是真的、假的或不确定的

一,
坦尼娅比埃里克大
克里夫比坦尼娅老
埃里克比克利夫大
如果前两条语句为true,则第三条语句为true

真的 假的 不确定 二,
蓝莓比草莓贵
蓝莓比覆盆子便宜
覆盆子比草莓和蓝莓贵
如果前两条语句为true,则第三条语句为true

真的 假的 不确定 三,
公园里所有的树都是开花的树
公园里的一些树是山茱萸
公园里所有的山茱萸都是开花的树
如果前两条语句为true,则第三条语句为true

真的 假的 不确定
这是Javascrpt

var score=0;
function check (name) {
    var methods = document.getElementsByName('name');
    for (var i=0; i<methods.length; i++) {
         if (methods[i].checked == true) {
             score +=1;
             alert(score);
            }
   }
}
var得分=0;
功能检查(名称){
var methods=document.getElementsByName('name');

对于HTML中的(var i=0;i,您试图在
onclick
事件中放置的
check()
函数中传递变量
a1
,而不是字符串
“a1”
,因此您应该替换:

onclick="check(a1)"
var methods = document.getElementsByName('name');
与:

在javascript中,您总是使用
name=“name”
查找元素,因此您应该替换:

onclick="check(a1)"
var methods = document.getElementsByName('name');
与:


那么,你的解决方案有什么问题吗?谢谢。我花了一个小时摆弄这些引文,但不知怎的没有弄明白。它现在可以工作了。