Jquery 如何使进度条返回单击返回按钮

Jquery 如何使进度条返回单击返回按钮,jquery,html,css,progress-bar,Jquery,Html,Css,Progress Bar,我想点击返回按钮,让进度条返回一步 function progress(){ var totalQuestions = 7; var currentQuestion = 0; var $progressbar = $("#progressbar"); $(".option").on("click", function(){ if (currentQuestion >= totalQuestions){ return; } currentQuestion++; $progres

我想点击返回按钮,让进度条返回一步

function progress(){
var totalQuestions = 7;
var currentQuestion = 0;
var $progressbar = $("#progressbar");

$(".option").on("click", function(){
  if (currentQuestion >= totalQuestions){ return; }
  currentQuestion++;
  $progressbar.css("width", Math.round(100 * currentQuestion / totalQuestions) + "%");
});

}

“返回”按钮将我带到前面的问题,但不会更改进度栏。如果在页面上添加一个id为的额外按钮,例如backButton,则可以执行以下操作:

$(".backButton").on("click", function(){
  currentQuestion--;
  $progressbar.css("width", Math.round(100 * currentQuestion / totalQuestions) + "%");
});

因此,您可以向另一个按钮添加一个额外的onClickListener,该按钮控制相同的progressbar,您可以将其放入完全相同的函数中,并在函数中全局声明变量。

我想这可能有助于使用类为
backward
的按钮:

function progress(){
var totalQuestions = 7;
var currentQuestion = 0;
var FinalCurrentQuestion = 0;
var $progressbar = $("#progressbar");

$(".option").on("click", function(){
  if (currentQuestion >= totalQuestions){ return; }
  currentQuestion++;
  FinalCurrentQuestion = currentQuestion;
  $progressbar.css("width", Math.round(100 * currentQuestion / totalQuestions) + "%");
});

$(".backward").on("click", function(){
  if (FinalCurrentQuestion == currentQuestion) {
  currentQuestion--;
  $progressbar.css("width", Math.round(100 * currentQuestion / totalQuestions) + "%");
  }
});

}

Daniel Vercoutren感谢您的回复。这个解决方案使进度条从一开始就很好,我的目标是后退一步