javascript - 重置进度条值 jQuery

标签 javascript jquery html css

我正在尝试在测验结束后重置测验中的进度条。

如果您点击下面的链接并:

  1. 回答问题
  2. 进度条根据一系列问题上升
  3. 测验结束
  4. 进度条保持在 100%

这是工作代码的链接 http://codepen.io/fun/pen/RRPdbb?editors=1010

增加进度条的部分是

  progress += questions.length * 100 / questions.length / questions.length;

  if ($('#progress').val() >= 100) {
    $('#progress').val(0);
  }
  $('#progress').val(progress);

然而,这只会增加而不会重置。

希望你能帮忙,这是代码。

  var test_status, options, pos = 0,
  question, optA, optB, optC, option_radio, user_guess, correct = 0,
  progress = 0;

var questions = [
  ['What is 4 * 4?', '16', '34', '18', 'A'],
  ['What is 80 / 20?', '49', '4', '3', 'B'],
  ['What is 760 - 160?', '650', '500', '600', 'C'],
  ['What is 330 + 150?', '480', '800', '400', 'A']
];
// Function to get ids
function _(x) {
  return document.getElementById(x);
};
// Function to ask the question
function askQuestion() {
  // End of quiz
  if (pos >= questions.length) {
    test_status.innerHTML = 'Test Complete. You scored ' + correct + ' out of ' + questions.length;
    options.innerHTML = '<br>' + 'To attempt the quiz again' + '<br><br>' + '<button onclick = "askQuestion()"> Click here</button>';
    pos = 0;
    correct = 0;
    return false;

  }

  // Get position of question
  question = questions[pos][0];
  // Get positions of options
  optA = questions[pos][1];
  optB = questions[pos][2];
  optC = questions[pos][3];
  // Get test status id
  test_status = _('test_status');
  // Get options id
  options = _('options');
  // Print question stage
  test_status.innerHTML = "<h2> You are on question " + (pos + 1) + " out of " + questions.length + "</h2>";
  // Print current question
  options.innerHTML = "<h3>" + question + "</h3>";
  // Print options for Q
  options.innerHTML += "<input type ='radio' name ='options' value ='A'> " + optA + "<br>";
  options.innerHTML += "<input type ='radio' name ='options' value ='B'> " + optB + "<br>";
  options.innerHTML += "<input type ='radio' name ='options' value ='C'> " + optC + "<br><br>";
  // Add a submit button
  options.innerHTML += "<button onclick ='checkAnswer()'>submit</buton>";

};
// Check answer 
function checkAnswer() {

  // Get name attr from raio 
  option_radio = document.getElementsByName('options');
  // loop through each radio
  for (var i = 0; i < option_radio.length; i++) {
    // check if option_radio is check
    if (option_radio[i].checked) {
      user_choice = option_radio[i].value;
    }
  }
  // check is correct
  if (user_choice == questions[pos][4]) {
    correct++;
    // increment progress based on questions length
  }
  pos++;

  progress += questions.length * 100 / questions.length / questions.length;

  if ($('#progress').val() >= 100) {
    $('#progress').val(0);
  }
  $('#progress').val(progress);

  askQuestion();

};

askQuestion();

最佳答案

这是一个逻辑错误。只需在检查之前设置该值。

  $('#progress').val(progress);
  if ($('#progress').val() >= 100) {
    $('#progress').val(0);
  }

关于javascript - 重置进度条值 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646752/

相关文章:

javascript - 简单的 JQUERY 数据检索不起作用

javascript - 用 jquery 选择器包装就地函数意味着什么?

Outlook 2016 中的 HTML CSS 布局

html - 为什么我的 div 图像没有出现在父 div 中?

javascript - 使用 Javascript/Jquery 用图像替换单词

javascript - 第一次加载 Javascript AngularJS 时不显示日期

javascript - 复选框状态更改不起作用

javascript - jQuery:淡入文本输入,保持可见直到不活动

javascript - 如何从多选下拉菜单中获取值并将其与 POST 一起发送?

php - WooCommerce - 按产品分组下载