gpt4 book ai didi

javascript - 将代码后面的进度百分比传递给 Jquery 进度条?

转载 作者:行者123 更新时间:2023-11-30 12:03:35 25 4
gpt4 key购买 nike

我想指定一个通用代码,这样即使我更改了 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)) + "%");
});

}

};
});
});
.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;
}
<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>

最佳答案

您的代码是意外运行的。

使用此代码初始化进度条(在文档 ready 事件中)

$( "#progressbar" ).progressbar(
{
max: allQuestions.length-1,
value: 0
});

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

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

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

更新的fiddle is here .

注意事项

  1. 您正在文档就绪 事件和点击 处理程序中使用$(function(){...})。这几乎没用。
  2. 旧代码可以正常工作,因为问题数 (10) 是 100(默认最大值)的平方根。

关于javascript - 将代码后面的进度百分比传递给 Jquery 进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35960847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com