Javascript 无法从函数访问数组

Javascript 无法从函数访问数组,javascript,Javascript,我现在正在创建一个有2个问题的测验网站。用户将获得一个问题和单选按钮来选择答案。用户选择的任何内容都将存储在cookie中,稍后将提供给php进行数据库检索。status数组用于检查是否选中了第一个选项或第二个选项。当我选择“上一个”按钮时,会显示上一个问题,但单选按钮不会改变,即它将与第二个问题相同 <html> <head> <script> var count = 1; var status = new Arra

我现在正在创建一个有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>

var计数=1;
var status=新数组();
函数calculateInput(){
if(document.getElementById('radio1')。选中){
状态[计数]=1;
ans1_value=document.getElementById('radio1').value;
document.cookie=计数+“=”+ans1_值+”;“
}否则
if(document.getElementById('radio2')。选中){
状态[计数]=2;
ans1_value=document.getElementById('radio2').value;
document.cookie=计数+“=”+ans1_值+”;“
}
}
函数myFunction(){
计数=计数+1;
如果(计数=2){
x=document.getElementById(“问题”);//查找元素
x、 innerHTML=“您的公关公司已被聘请为您所在城市的一家历史悠久的豪华酒店重新开业做宣传。除了制作开幕式晚会活动外,您的团队还将为当晚创建一个主题。您是否愿意:;//更改内容
document.getElementById('Answer0').firstChild.nodeValue='了解更多关于酒店历史的信息,以发展主题(注意细节)';
document.getElementById('Answer1').firstChild.nodeValue='研究翻新团队如何保存建筑的原始细节,以纳入活动的新闻资料袋中。'(研究)';
document.getElementById(“radio1”).checked=true;
document.getElementById(“radio1”).value=“321”;
document.getElementById(“radio2”).value=“311”;
}否则,如果(计数>2){
window.location.href=”http://localhost/quiz/same_page_implementation/final.php";
}
}
函数calculatePrev(){
计数=计数-1;
如果(计数=1){
x=document.getElementById(“问题”);//查找元素
x、 innerHTML=“您是一家建筑公司的项目经理,该公司正在设计世界上最大的摩天轮,它将矗立在圣保罗的天际线上。您最想处理以下哪项任务?”;//更改内容
document.getElementById('Answer0').firstChild.nodeValue='每周与项目负责人会面,以确保满足安全要求和预算目标。(规划)';
document.getElementById('Answer1').firstChild.nodeValue='每周与项目的世界著名建筑师会面,向他简要介绍团队的进度。(管理)';
如果(状态[计数]==1){
document.getElementById(“radio1”).checked=true;
}否则如果(状态[计数]==2){
document.getElementById(“radio2”).checked=true;
}
document.getElementById(“radio1”).value=“325”;
document.getElementById(“radio2”).value=“310”;
}
}

你是一家建筑公司的项目经理,该公司正在设计世界上最大的摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮摩天轮。您最想处理以下哪项任务?

每周与项目领导会面,确保安全要求和预算目标得到满足。(规划)
每周与项目的世界著名建筑师会面,向他简要介绍团队的进展情况。(行政)
下一个 以前的
将“status”数组重命名为任何其他名称。 问题是:当你写作的时候

var status = []
它与
window.status=[]
相同

Window.status是。 它在大多数浏览器中被禁用,但仍然存在,无法覆盖


我尝试了
status1
,但它似乎起了作用。

你把这个问题弄得太复杂了。我会删除cookies,只在提交时获取信息,无论是get还是post。但我不希望在用户提交答案时重新加载整个页面。