私は、コードを次のようにあなたのために作られた一例です。 申し訳ありませんが、私はccsルールを作る時間がありませんでしたが、あなたは質問が混ざり合っていて、それらの答えがすべて混じっていることが分かります。 http://devel.vis25.com/test.php
私はたとえば、あなたがここでjQueryとjQueryのテンプレートに
が必要になります、このようなものを使用することをお勧めいたしますjQueryのへのリンクはここjquery tempaltes
をダウンロードしているyou'r tempaltesの一例であるとhtml 。
<html>
<head>
<script src="https://devel.vis25.com//Vendors/JqueryUI/external/jquery/jquery.js"></script>
<script src="http://devel.vis25.com/Vendors/jquery.tmpl.min.js"></script>
</head>
<body onload="RenderQuestions();">
<div id="Questions"></div>
<script id="Question-Tempalte" type="text/x-jQuery-tmpl">
<div class="Question" id=question-"${ID}">
<div class="Question-text">${QuestionText}</div>
<div class="Question-answer-container" id="Question-answer-container-${ID}"></div>
</div>
</script>
<script id="Answer-Tempalte" type="text/x-jQuery-tmpl">
<div class="answer" id="answer-${ID}">
<div class="answer-text" tabIndex="${ID}">${answerText}</div>
</div>
</script>
</body>
</html>
javascriptでこれを行う。
//Function that is called in body 'onload' event.
function RenderQuestions(){
//Array of you'r questions as json objects
var questions = [
{ ID : '1', QuestionText : 'What is the weather like?' },
{ ID : '2', QuestionText : 'What time of day is it?' },
{ ID : '3', QuestionText : 'Whats your favourite music?' },
{ ID : '4', QuestionText : 'Which season is your favourite?' },
{ ID : '5', QuestionText : 'What colour are your eyes?' },
];
//Call shuffle function for your questions, so they are mixed randomly.
var ShuffledQuestions = shuffle(questions);
//Loop true all of your questions and render them inside of your questions <div>
//Allso call functions 'RenderAnswers()' by question id value[ 'ID' ].
$.each(ShuffledQuestions, function(index, value){
$('#Question-Tempalte').tmpl(value).appendTo('#Questions');
RenderAnswers(value[ 'ID' ]);
});
}
//Shuffle function return randomly mixed array.
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
//RenderAnswers function takes QuestionID as argument so we can render answer elements for right questions, and we have right answers.
function RenderAnswers(QuestionID){
var Answers = [];
//Check which question are we rendering.
//Answers for question ID 1 ('What is the weather like?').
if(QuestionID == 1){
Answers = [
{ AnswersID : 1 , answerText : 'Sunny' },
{ AnswersID : 2 , answerText : 'Raining'},
{ AnswersID : 3 , answerText : 'Cloudy'},
{ AnswersID : 4 , answerText : 'Windy'},
];
}
//Answers for question ID 2 ('What time of day is it?').
if(QuestionID == 2){
Answers = [
{ AnswersID : 1 , answerText : '8:00' },
{ AnswersID : 2 , answerText : '12:00'},
{ AnswersID : 3 , answerText : '18:00'},
{ AnswersID : 4 , answerText : '00:00'},
];
}
//Answers for question ID 3 ('Whats your favourite music?').
if(QuestionID == 3){
Answers = [
{ AnswersID : 1 , answerText : 'Rock' },
{ AnswersID : 2 , answerText : 'pop'},
{ AnswersID : 3 , answerText : 'rap'},
{ AnswersID : 4 , answerText : 'EDM'},
];
}
//Answers for question ID 4 ('Which season is your favourite?').
if(QuestionID == 4){
Answers = [
{ AnswersID : 1 , answerText : 'Summer' },
{ AnswersID : 2 , answerText : 'Winter'},
{ AnswersID : 3 , answerText : ''},
{ AnswersID : 4 , answerText : ''},
];
}
//Answers for question ID 5 ('What colour are your eyes?').
if(QuestionID == 4){
Answers = [
{ AnswersID : 1 , answerText : 'blue' },
{ AnswersID : 2 , answerText : 'brown'},
{ AnswersID : 3 , answerText : 'green'},
{ AnswersID : 4 , answerText : ''},
];
}
//Shuffle answers.
var ShuffledAnswers = shuffle(Answers);
//Renders answer elements for question.
$('#Answer-Tempalte').tmpl(ShuffledAnswers).appendTo('#Question-answer-container-'+QuestionID);
}
私はあなたを助けることができたホープは、と何でもお気軽に、私は右のあなたの質問を理解していなかったです!
敬具、 Vis25
してください[いくつかの検索を行う](https://www.google.com/search?q=site%3Astackoverflow.com+random+questions+quiz+javascript)の前に尋ねますここでの質問。ここにはランダムな質問/回答のクイズに関する文字通り何百もの質問があります。 –