无法从函数访问数组

时间:2014-03-23 15:23:16

标签: javascript

我现在正在创建一个包含2个问题的测验网站。将为用户提供问题和单选按钮以选择答案。用户选择的任何内容都将存储在cookie中,稍后将提供给php进行数据库检索.status数组用于检查是否选中了第一个选项或第二个选项。当我选择上一个按钮时,上一个问题会被显示,但单选按钮不会改变,这与第二个问题相同。

<html>

<head>
    <script>
        var count = 1;
        var status = new Array();

        function calculateInput() {
            if (document.getElementById('radio1').checked) {
                status[count] = 1;
                ans1_value = document.getElementById('radio1').value;
                document.cookie = count + "=" + ans1_value + ";"



            } else
            if (document.getElementById('radio2').checked) {
                status[count] = 2;
                ans1_value = document.getElementById('radio2').value;
                document.cookie = count + "=" + ans1_value + ";"


            }
        }

        function myFunction() {


            count = count + 1;
            if (count == 2) {
                x = document.getElementById("questions"); // Find the element
                x.innerHTML = "Your PR firm has been hired to promote the reopening of a historic luxury hotel in your town. In addition to producing the opening night gala event, your team will be creating a theme for the evening. Would you rather:"; // Change the content
                document.getElementById('Answer0').firstChild.nodeValue = 'Discover more about the hotel’s history to develop the theme. (Attention to detail)';
                document.getElementById('Answer1').firstChild.nodeValue = 'Research how the renovation team preserved the building’s original details for inclusion in the event’s press kit. (Research)';
                document.getElementById("radio1").checked = true;
                document.getElementById("radio1").value = "321";
                document.getElementById("radio2").value = "311";
            } else if (count > 2) {
                window.location.href = "http://localhost/quiz/same_page_implementation/final.php";
            }
        }

        function calculatePrev() {
            count = count - 1;
            if (count == 1) {
                x = document.getElementById("questions"); // Find the element
                x.innerHTML = "You are a project manager in an architecture firm that’s designing the world’s largest Ferris Wheel—one that will tower over the São Paulo skyline. Which of the following tasks would you most want to handle?"; // Change the content
                document.getElementById('Answer0').firstChild.nodeValue = 'Meeting with the project’s leaders weekly to ensure safety requirements and budget targets are being met. (Planning)';
                document.getElementById('Answer1').firstChild.nodeValue = 'Meeting each week with the project’s world-famous architect to brief him on the team’s  progress. (Administration)';
                if (status[count] == 1) {
                    document.getElementById("radio1").checked = true;
                } else if (status[count] == 2) {
                    document.getElementById("radio2").checked = true;
                }


                document.getElementById("radio1").value = "325";
                document.getElementById("radio2").value = "310";
            }
        }
    </script>
</head>

<body>
    <p id="questions">
        You are a project manager in an architecture firm that’s designing the world’s largest Ferris Wheel—one that will tower over the São Paulo skyline. Which of the following tasks would you most want to handle?
    </p>

    <input type="radio" name="question1" value="325" id="radio1">
    <label for="radio1" id="Answer0">Meeting with the project’s leaders weekly to ensure safety requirements and budget targets are being met. (Planning)</label>
    <br>
    <input type="radio" name="question1" value="310" id="radio2">
    <label for="radio2" id="Answer1">Meeting each week with the project’s world-famous architect to brief him on the team’s progress. (Administration)</label>
    <br>


    <button type="button" onclick=" calculateInput(); myFunction()">Next</button>
    <button type="button" onclick=" calculatePrev()">Previous</button>

</body>

</html>

1 个答案:

答案 0 :(得分:1)

将“status”数组重命名为任何其他名称。 问题是:当你写

var status = []

同样适用于window.status = []

Window.status是http://www.w3schools.com/jsref/prop_win_status.asp。 它在大多数浏览器中都被禁用,但仍然存在且无法覆盖。

我尝试了status1,似乎有效。