1

这是我的代码:

$(document).ready(function () {
    $("input").focus(function () {
        $(this).css('outline-color', '#559FFF');
        $(this).blur(function () {
            $(this).css("outline-color", "#FF0000");
        });
    });
    $("input").click(function () {
        var value = $(this).val(function () {
            $(this).html("");
        });
    });
    $(".awesome").click(function () {
        var toStore = $("input[name=name]").val();
        if (!/^[A-Za-z]+ [A-Za-z]+$/.test(toStore)) {
            alert("You Must Put a Valid Name");
        } else {
            $("#contain").children().fadeOut(1000);
            $("#contain").delay(1000).queue(function () {
                $("#contain").append("<h1>Welcome to My Quiz : " + toStore + "</br>" +
                    "Youll Get 10 Questions To Answer </br> " +
                    "Here Is the First One:Who is Prime Minister of the United Kingdom? </h1>");

                var allQuestions = {
                    outquestions:{
                        question1 : {    quest: "Who is Prime Minister of the United Kingdom?",
                            choices: ["David Cameron","Gordon Brown","Winston Churchill","Tony Blair"],
                            correctAnswer: "David Cameron"},
                        question2 : {    quest: "whats dad name?",
                            choices: ["David ","Gordon ","Winston ","Tony "],
                            correctAnswer: "David"}
                    },
                    correctAnswers: 0
                };

                var outquestions = allQuestions["outquestions"];
                for (var question in outquestions) {
                    $("#contain").append("<p>(outquestions[question][quest]</p>");
                    for (var choice in outquestions[question]["choices"]) {
                        $("h1").append("<form><input type='radio' name=question value=choice></form>");
                    }
                }
                $("#contain").append("<form><input type='submit' value='submit'></form>");
                $("#contain").on("click", "input[name=submit]", function () {
                });

            });
        }
    });
});

我希望本节循环遍历问题并在以下内容之间写:

var outquestions = allQuestions["outquestions"];
                    for (var question in outquestions) {
                        $("#contain").append("<p>(outquestions[question][quest]</p>");
                        for (var choice in outquestions[question]["choices"]) {
                            $("h1").append("<form><input type='radio' name=question value=choice></form>");
                        }
                    }
                    $("#contain").append("<form><input type='submit' value='submit'></form>");

之后,我想要所有已循环的问题的答案任何给定的时间来显示单选按钮和单选按钮按钮的值,我得到你在图片中看到的内容。这里有什么问题?在此处输入图像描述

4

2 回答 2

1

尝试这样的事情:

var outquestions = allQuestions["outquestions"];
    for (var question in outquestions) {
        $("#contain").append("<p>(outquestions[question][quest]</p>");
        for (var choice in outquestions[question]["choices"]) {
            $("#contain").append("<h1><input type='radio' name=question value=choice></h1>");
        }
    }
    $("#contain").append("<input type='submit' value='submit'>");
    $("#contain").on("click", "input[name=submit]", function () {});

我没有测试上面的代码,但它应该把你的选择放在你的问题之间。

于 2013-04-07T10:01:07.687 回答
0

我为你们做了一些改变,我的一些想法是我的代码的结果:

var allQuestions = {
                    outquestions:{
                        question1 : {    quest: "Who is Prime Minister of the United Kingdom?",
                            choices: ["David Cameron","Gordon Brown","Winston Churchill","Tony Blair"],
                            correctAnswer: "David Cameron"}
                    },
                    correctAnswers: 0
                };

                var outquestions = allQuestions["outquestions"];
                for (var question in outquestions) {
                    $("#contain").append("<h1>" + outquestions[question]['quest'] + "</h1>");
                    for (var choice in outquestions[question]["choices"]) {
                        $("#contain").append("<p><input type='radio' name=question value=choice>" +outquestions[question]["choices"][choice] + "</p>");
                    }
                }

感谢您的帮助。

于 2013-04-07T10:28:51.143 回答