Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.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/2/jquery/81.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 使用jQuery存储单选按钮输入的数值_Javascript_Jquery_Html_Forms_Radio Button - Fatal编程技术网

Javascript 使用jQuery存储单选按钮输入的数值

Javascript 使用jQuery存储单选按钮输入的数值,javascript,jquery,html,forms,radio-button,Javascript,Jquery,Html,Forms,Radio Button,我正在做一个HTML5测验,我使用单选按钮回答yes或no问题,然后给每个单选按钮分配一个0或1的值,然后将数值存储为变量,然后将所有变量相加,为测验打分 更新 嘿!我做了您建议的更改,但当我调用变量时,它仍然显示为未定义: 这是我的html: <form class="answer" id="question_one"> <input type="radio" class="styled" name="first_answer" value="1"/><!--

我正在做一个
HTML5
测验,我使用单选按钮回答
yes
no
问题,然后给每个单选按钮分配一个
0
1
的值,然后将数值存储为变量,然后将所有变量相加,为测验打分

更新

嘿!我做了您建议的更改,但当我调用变量时,它仍然显示为未定义:

这是我的html:

<form class="answer" id="question_one">
  <input type="radio" class="styled" name="first_answer" value="1"/><!-- POSITIVE ANSWER -->
  <div class="answer_text">Yes</div>
  <input type="radio" class="styled" name="first_answer" value="0" /><!-- NEGATIVE ANSWER -->
  <div class="answer_text">No</div>
</form>
})); 我试图通过只针对name=first\u asnwer的单选按钮来为jQuery对象增加一点特殊性。也许我搞砸了



我认为您这里的问题是理解
$(document).ready()
函数。此函数在加载页面的所有依赖项(CSS、JavaScript等)后执行

在执行此函数时,您已经设置了变量,这可能是在您单击第一个单选按钮之前!这里的解决方案是只在用户完成回答后计算值。可能在无线电元件发生变化时-

$(document).ready(function () {
  $('input:radio').on('change',function(){
    var $first_question = parseInt($("input[name=first_answer]:checked").val());
    var $second_question = parseInt($("input[name=second_answer]:checked").val());
    $first_question = $first_question? $first_question : 0; 
    $second_question = $second_question? $second_question : 0;
    var $total_score = $first_question + $second_question;
    alert($total_score);
  });
});

另一方面,在HTML中,两个输入的名称都是“first_answer”。@are-这就是允许它们充当单选按钮的原因。如果没有相同的组(由“名称”属性定义),则可以单击它们,并且不具有打开/关闭的切换效果。嗯,单选按钮需要具有相同的名称才能相互排斥。这样,用户就不能选择是和否。@are-你可以看到区别是的抱歉误解了他的设置,我认为第二个广播框是对一个问题的单独回答。因为jQuery正在引用第二个答案。@are-haha。。。这也是第二次尝试;)嘿谢谢你的帮助!我做了您建议的更改,但当我调用var first_question时,它仍然是未定义的。我已经用新代码更新了这个问题。@laz-您必须确保两组单选按钮实际上都有
:选中的
值。这里发生的事情是,当您单击第一个单选元素时,它也试图计算第二个答案的值,但还没有值。@laz-查看我答案的更新。您所需要做的就是确保为获得最终分数而添加的值都存在。@laz-很高兴为您提供帮助。。。快乐编码!
$(document).ready(function () {
  $('input:radio').on('change',function(){
    var $first_question = parseInt($("input[name=first_answer]:checked").val());
    var $second_question = parseInt($("input[name=second_answer]:checked").val());
    $first_question = $first_question? $first_question : 0; 
    $second_question = $second_question? $second_question : 0;
    var $total_score = $first_question + $second_question;
    alert($total_score);
  });
});