将代码中的进度百分比传递给Jquery进度条?

时间:2016-03-12 17:18:14

标签: javascript jquery html css jquery-ui

我想指定一个通用代码,这样即使我更改了allQuestions数组中的对象数,进度条也会在最后一个问题上完成。 这是JSFiddle。 这是相关的代码:

$(function() {
    $( "#progressbar" ).progressbar({
      value: currentquestion*(allQuestions.length)
    });
  });



var allQuestions = [{
    question: "Who is the Prime Minister of the United Kingdom?",
    choices: ["David Cameron", "Gordon Brown", "Winston Churchill"],
    correctAnswer: 0
  },

  {
    question: "Who is the Prime Minister of India?",
    choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"],
    correctAnswer: 2
  },

  {
    question: "Who is the Prime Minister of America?",
    choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"],
    correctAnswer: 1
  },

  {
    question: " Who averaged one patent for every three weeks of his life?",
    choices: ["Thomas Edison", "Tesla", "Einstein"],
    correctAnswer: 0
  },

  {
    question: "What continent is cut into two fairly equal halves by the Tropic of Capricorn?",
    choices: ["Africa", "South America", "Australia"],
    correctAnswer: 2
  },

  {
    question: "What explorer introduced pigs to North America?",
    choices: ["Christopher Columbus", "Galileo", "Mussorie"],
    correctAnswer: 0
  },

  {
    question: "What F-word is defined in physics as a “nuclear reaction in which nuclei combine to form more massive nuclei”? ",
    choices: ["Furnace", "Fusion", "Fission"],
    correctAnswer: 1
  },

  {
    question: "What was the first planet to be discovered using the telescope, in 1781?",
    choices: ["Uranus", "Jupiter", "Mars"],
    correctAnswer: 0
  },

  {
    question: "Who is the chief minister of West Bengal, India?",
    choices: ["Buddhadev Bhattacharya", "Jyoti Basu", "Mamta Banerjee"],
    correctAnswer: 2
  },

  {
    question: "Which is the fastest growing religion in the world?",
    choices: ["Islam", "Christianity", "Hinduism"],
    correctAnswer: 0
  }
  /*
  	{
  		question: "Who is the Prime Minister of America?",
  		choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
  		correctAnswer:1
  	},

  	{
  		question: "Who is the Prime Minister of America?",
  		choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
  		correctAnswer:1
  	},

  	{
  		question: "Who is the Prime Minister of America?",
  		choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
  		correctAnswer:1
  	},

  	{
  		question: "Who is the Prime Minister of America?",
  		choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
  		correctAnswer:1
  	},

  	{
  		question: "Who is the Prime Minister of America?",
  		choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
  		correctAnswer:1
  	}	*/
];


$(document).ready(function() {
  var currentquestion = 0;
  var correctAnswers = 0;

  $(function() {
    $("#progressbar").progressbar({
      value: 0
    });
  });

  $('#question').html(allQuestions[currentquestion].question);
  $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
  $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
  $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");

  $("#next").click(function() {
    if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
      correctAnswers++;
    };
    currentquestion++;
    $(function() {
      $("#progressbar").progressbar({
        value: currentquestion * (allQuestions.length),
      });
    });
    if (currentquestion < allQuestions.length) {
      $('#question').html(allQuestions[currentquestion].question);
      $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
      $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
      $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
      if (currentquestion == allQuestions.length - 1) {
        $('#next').html("Submit");
        //$('#next').off("click");
        $('#next').click(function() {
          //	if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){
          //		correctAnswers++;
          //	};
          //	alert(correctAnswers);
          alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers * 100 / (currentquestion)) + "%");
        });

      }

    };
  });
});
&#13;
.ui-widget-header {
  background-image: none !important;
  background-color: #FF7860 !important; //Any colour can go here
}
label {
  display: inline-block;
  text-align: center;
}
/*input[name="option"] {
  float:left;
}*/

#progressbar {
  margin: auto;
  margin-top: 20px;
  float: none;
  width: 50%;
  /*height: 100%;*/
}
#container {
  text-align: center;
}
span {
  margin: 5em;
  padding: 3em;
}
.button {
  display: inline-block;
  padding: 1em;
  background-color: #79BD9A;
  text-decoration: none;
  color: white;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<div id="container">
  <h3 id="question"></h3>
  <form id="form">
    <input type="radio" name="option" value="0" id="option0" checked>
    <label for='option0'></label>
    <input type="radio" name="option" value="1" id="option1">
    <label for='option1'></label>
    <input type="radio" name="option" value="2" id="option2">
    <label for='option2'></label>
  </form>

  <br/>
  <a href="#" id="next" class="button">Next</a>
  <br/>
  <div id="progressbar"></div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

您的代码无意中正在使用。

使用此代码

初始化进度(在文档 ready 事件中)栏
$( "#progressbar" ).progressbar(
{
    max:   allQuestions.length-1,
    value: 0
});

这告诉它value必须是介于0和allQuestions.length-1之间的数字,并且当它这样做时,百分比从0%变为100%。

然后在更新进度条时(在 #next 元素的单击处理程序中),只需使用:

$( "#progressbar" ).progressbar(
{
   value: currentquestion,
});

更新后的fiddle is here

备注

  1. 您正在文档 ready 事件中使用$(function(){...}),并在单击处理程序中使用words = [] while time.time() - start < timeOut: Giveword = input("%s %10s%s" %("Enter a word using ", toPrint,": ")) words.append(Giveword) print("Time's Up!") print("Here are your words:", words) 。这几乎没用。
  2. 旧代码正在运行,因为问题(10)的数量是100的平方根(默认最大值)。