单击选择器以选择多个单选按钮问题

问题描述 投票:0回答:1

我正在尝试进行分数测验,请在此处输入:https://codepen.io/stdobrescu/pen/xxbMbLK

现在我有

var score = 0;
var scoreInt = 0;
$("#score").html(score);

$("input[type='radio']").click(function(){
scoreValue = $("input[type='radio']:checked").val();
scoreInt = parseInt(scoreValue, 10);
score += scoreInt;
console.log(scoreInt);
  console.log(score);
  $("#score").html(score);
  return scoreInt;
});

其中scoreInt是每个答案的Int值,而得分是总得分。问题是我的scoreInt不会随每个问题更新,而是从第一个问题获取值。我应该以某种方式按名称选择每个问题吗?是否需要for循环才能遍历所有问题?

[此外,如果用户返回问题并选择另一个值,关于如何更新分数的任何想法?当滚动到特定问题时,我正在考虑从score中减去scoreInt。

javascript jquery codepen
1个回答
0
投票

代码的问题是scoreValue = $("input[type='radio']:checked").val();,您应该使用scoreValue = $(this).val();

var score = 0;
var scoreInt = 0;
$("#score").html(score);

$("input[type='radio']").click(function(){
scoreValue = $(this).val();
scoreInt = parseInt(scoreValue, 10);
score += scoreInt;
console.log(scoreInt);
  console.log(score);
  $("#score").html(score);
  return scoreInt;
});
© www.soinside.com 2019 - 2024. All rights reserved.