Cannt获得全局变量temp的值

时间:2015-10-30 09:03:34

标签: javascript html

我正在创建在线考试网站

使用ajax从question.json文件中提取问题 它的数组长度存储在临时全局变量
中 我可以在start.click函数中打印它但无法在 next.click函数

中打印或显示它
 var questionid = [];
var answer = [];
var pos = 0;
var temp;

$("document").ready(
        function() {


            $("#box").hide();

            $('#pos').text(pos);

            $("#start").click(function() {
                $("#box").show();
                $.getJSON("question.json", function(jd) {

                    temp=jd.length;
                    $("#dbsize").text(temp);


                    $('#questionid').text(jd[pos].questionid);

                    $('#dis').text(jd[pos].question);
                    $("#res1").text(jd[pos].questionid);
                    $('#a').text(jd[pos].a);
                    $('#b').text(jd[pos].b);
                    $('#c').text(jd[pos].c);
                    $('#d').text(jd[pos].d);
                    // STORE QUESTION ID INTO ARRAY "QUESTION ID"
                    questionid.push(jd[pos].questionid);
                    //pos++;
                    $("#start").hide();

                });

            });

            $("#next").click(function() {

                // STORE ANSWER INTO ARRAY "ANSWER"
                var selected = $("input[type='radio'][name='option']:checked");
                answer.push(selected.val());
                $('#res1').text(selected.val());

                // CLEAR RADIO BUTTON
                $('input[name=option]').attr('checked', false);
                $('#temp').text(temp.val());
                $.getJSON("question.json", function(jd) {
                    //temp=jd.length;
                    if(pos<temp){

                        $('#pos').text(pos);
                        pos++;

                        $('#questionid').text(jd[pos].questionid);

                        $("#res1").text(jd[pos].questionid);

                        $('#dis').text(jd[pos].question);
                        $('#a').text(jd[pos].a);
                        $('#b').text(jd[pos].b);
                        $('#c').text(jd[pos].c);
                        $('#d').text(jd[pos].d);

                        // STORE QUESTION ID INTO ARRAY "QUESTION ID"
                        questionid.push(jd[pos].questionid);

                    }                                                                                                                                   
                });



                // DISPLAYING TABLE
                var temp = "  ";
                var size = answer.length;
                $("#size").text(size);
                var table = "<table><tr><th>Id</th></tr>";
                for (var n = 0; n < size; n++) {
                    table += "<tr><td>" + questionid[n] + "</td></tr>";
                }
                table += "</table>";
                $('#res2').html(table);
            });

            $("#prev").click(
                    function() {
                        if (pos >= 1) {
                            pos--;
                        }
                        $.getJSON("question.json", function(jd) {

                            // CEHCKING ANSWERS
                            var value = answer[pos];
                            $("input[name=option][value=" + value + "]").prop(
                                    'checked', true);

                            $('#pos').text(pos);

                            $('#questionid').text(jd[pos].questionid);
                            $("#res1").text(jd[pos].questionid);
                            $('#dis').text(jd[pos].question);
                            $('#a').text(jd[pos].a);
                            $('#b').text(jd[pos].b);
                            $('#c').text(jd[pos].c);
                            $('#d').text(jd[pos].d);

                        });

                        var temp = "  ";
                        var size = answer.length;
                        $("#size").text(size);
                        var table = "<table><tr><th>Id</th><th>Ans</th></tr>";
                        for (var n = 0; n < size; n++) {
                            table += "<tr><td>" + questionid[n] + "</td>"
                                    + "<td>" + answer[n] + "</td></tr>";
                        }
                        table += "</table>";
                        $('#res2').html(table);
                    });

            $("#result").click(
                    function() {

                        var temp = "  ";
                        var size = answer.length;
                        $("#size").text(size);
                        var table = "<table><tr><th>Id</th><th>Ans</th></tr>";
                        for (var n = 0; n < size; n++) {
                            table += "<tr><td>" + questionid[n] + "</td>"
                                    + "<td>" + answer[n] + "</td></tr>";
                        }
                        table += "</table>";
                        $('#res2').html(table);

                    });

        });


**<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="css/mystyle.css">
<script src="JS_folder/jquery-2.1.4.min.js"></script>
<script src="javascript/myscript.js"></script>
</head>
<body>
    <div id="box" class="tab">
        <div id="question">
            <div id="questionid">ID</div>
            <p id="txt">ANS</p>
            <p id="dis">Question</p>
        </div>
        <div id="answers">
            <table id="table1">
                <tr>
                    <td><input type="radio" name="option" value="a" id="a1"></td>
                    <td><p id="a"></td>
                </tr>

                <tr>
                    <td><input type="radio" name="option" value="b" id="b1"></td>
                    <td><p id="b"></td>
                </tr>

                <tr>
                    <td><input type="radio" name="option" value="c" id="c1"></td>
                    <td><p id="c"></td>
                </tr>

                <tr>
                    <td><input type="radio" name="option" value="d" id="d1"></td>
                    <td><p id="d"></td>
                </tr>
            </table>

        </div>
        <button id="next">Next</button>     
        <button id="prev">Previous</button> 
        <div id="res1">res1</div>
        <table border="1" id="res2">Question id With Answer Table</table>
        <hr>
        DataBase Size::<div id="dbsize"></div>
        TEMP::<div id="temp"></div>
        POS::<div id="pos"></div>
        <hr>
        <button id="result">Result</button>
    </div>
    <button id="start">Start</button>

</body>
</html>**

1 个答案:

答案 0 :(得分:0)

您似乎正在重新定义每个函数中的temp var,而不是使用您在顶部定义的全局temp var。当你指定&#34; &#34;在函数内部的temp,函数的变量范围而不是全局函数。

尝试temp =&#34; &#34;

代替。

相关问题