将动态测验中的响应与jquery进行比较

时间:2013-03-29 00:01:54

标签: javascript jquery html json

嗨,我总是在jquery上努力尝试精益求精,所以我决定做一个动态的测验,以提高我的技能,我一直在做一步,然后尝试将它包裹起来。我被困在如何检查我的复选框之一被检查以比较是否正确的答案,然后更新问题和答案。这是我的HTML:

<!DOCTYPE html>
<html lang='es'>
<head>
<meta charset='UTF-8'>
<title>Dynamic Quiz</title>
<link rel="stylesheet" type="text/css" href="css/normalize.css">
<link rel="stylesheet" type="text/css" href="css/estilos.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/prefixfree.js"></script>
<script type="text/javascript" src="js/codigo.js"></script>
</head>
<body>
<header>
    <hgroup>
        <h1>This is my Dynamic Quiz</h1>
        <h2>Using html5 / css / javascript</h2>
    </hgruop>
</header>

<section id='description'>
    <p>This quiz is compossed by 10 questions, you have to answer at least 7
       from 10 to pass the exam.</p>
    <h2>Lets start!</h2>
</section>

<div id='questions-number'>
    <p>Question <span id='current-question'>1</span> of <span>10</span> </p>
</div>

<section id='questions'>
    <p id='question'></p>

    <ul>
        <li><input type='checkbox' id='checkAnswer0'/><label id='answer0'>answer0</label></li>
        <li><input type='checkbox' id='checkAnswer1'/><label id='answer1'>answer1</label></li>
        <li><input type='checkbox' id='checkAnswer2'/><label id='answer2'>answer2</label></li>
        <li><input type='checkbox' id='checkAnswer3'/><label id='answer3'>answer3</label></li>
    </ul>
</section>

<div id='next'>
    next
</div>
</body>
</html>

这是我到目前为止在jquery中得到的:)

$(document).on('ready', ready);
function ready(){
var allQuestions =
[
    {
        question1: "Whats my real name?",
        choices1: ["Jhonnatan", "Alberto", "Tatan","Jaime"],
        answer1: 0
    },

    {
        question2: "Who is Colombia's president?",
        choices2: ["Alvaro Uribe", "Andres Pastrana", "Juan Manuel Santos","Tatan"],
        answer2: 2
    },

    {
        question3: "My favorite super heroe?",
        choices3: ["Batman", "Flash", "Tatan","Javascript"],
        answer3: 3
    },

    {
        question4: "Wich sports do i practice?",
        choices4: ["Climbing", "Swimming", "Programming","Running"],
        answer4: 0
    },

    {
        question5: "Whats my dad's name?",
        choices5: ["Alberto", "Jorge", "Javier","Jose"],
        answer5: 1
    },

    {
        question6: "Whats my favorite color?",
        choices6: ["Red", "Purple", "Blue","All"],
        answer6: 2
    },

    {
        question7: "My favorite alcoholic drink",
        choices7: ["Vodka", "Aguardiente", "Rum","Tekila"],
        answer7: 3
    },

    {
        question8: "Whats my favorite kind of music?",
        choices8: ["Hardcore", "Reggaeton", "Salsa","Programming"],
        answer8: 0
    },

    {
        question9: "How many qestions has this quiz??",
        choices9: ["20", "8", "10","12"],
        answer9: 2
    },

    {
        question10: "My favorite programming lenguage?",
        choices10: ["Ruby", "Arduino", "Python","Javascript"],
        answer10: 3
    }
];
var question = $('#question');
var choice1 = $('#answer0');
var choice2 = $('#answer1');
var choice3 = $('#answer2');
var choice4 = $('#answer3');

question.text(allQuestions[0].question1);
choice1.text(allQuestions[0].choices1[0]);
choice2.text(allQuestions[0].choices1[1]);
choice3.text(allQuestions[0].choices1[2]);
choice4.text(allQuestions[0].choices1[3]);
console.log('funcionando!');

var next = $('#next');
next.on('click', changeQuestion);   
function changeQuestion(){
    var checked = $('#checkAnswer0');
    if($checked.prop('checked')){
        console.log('correcto')else{
            console.log('no es correctp'); 
        }
    }
}
}
如果你帮我这个,我将非常感谢你的帮助! :)

2 个答案:

答案 0 :(得分:1)

很高兴你正在努力学习。您可以通过以下几种方式找到已检查的元素,例如$(elem).is(':checked')$(':checked')以查找所有内容。

答案 1 :(得分:1)

This example似乎正在做你想要完成的事情。我试图尽可能少地修改你的代码,所以希望这是有道理的!

例如,当密钥一致时,迭代对象/数组要容易得多。

{
    question: "Whats my real name?",
    choices: ["Jhonnatan", "Alberto", "Tatan","Jaime"],
    answer: 0
},

... instead of ...

{
    question1: "Whats my real name?",
    choices1: ["Jhonnatan", "Alberto", "Tatan","Jaime"],
    answer1: 0
},