Arrays 将用户输入插入数组

Arrays 将用户输入插入数组,arrays,input,Arrays,Input,今天我的HTML/JS文件出现了这个问题,我正在尝试让脚本正常工作 <head> <h1>Pushing into an array</h1> </head> <body> <p>Enter your input to the form before adding</p> <p>Click to add to array</p> <p id

今天我的HTML/JS文件出现了这个问题,我正在尝试让脚本正常工作

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>
HTML:

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>

这样做的目的是从表单/警报中获取用户输入,并将其传输到一个数组(显示在屏幕上),但我似乎无法让它工作!有什么想法吗?

您对myFunction()的一次单击可能应该是display(),并且document.getElementById(“myFunction”)当前没有引用页面中任何元素的id,如果这是“demo”(不确定您是否试图将数组设置为页面中的某个元素)?

我不知道您到底想做什么,但是根据我的直觉,我修改了代码,按照你的要求去做

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>
HTML 添加一个值,单击输入值,然后单击显示变量。你会把它列出来的

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>

这是。

我在帮助下成功地对代码进行了排序。谢谢,修正了:

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>
<!DOCTYPE html>

<html>

<head>
    <script>
        var colours = ["Red", "Orange", "Yellow", "Green"];

        function display() {
           document.getElementById("demo").innerHTML = colours;
        }

        function setValue(){ 
           var myVariable = document.myform.text1.value;
           document.getElementById("demo2").innerHTML = myVariable;
           colours.push(myVariable);
        }
    </script>
</head>

<body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br> 
    <div id="myFunction"></div>

    <button onclick="display()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="demo2"></p>

</body>
</html>

变量颜色=[“红色”、“橙色”、“黄色”、“绿色”];
函数显示(){
document.getElementById(“demo”).innerHTML=colors;
}
函数setValue(){
var myVariable=document.myform.text1.value;
document.getElementById(“demo2”).innerHTML=myVariable;
颜色。推送(myVariable);
}
在添加之前,在表单中输入您的输入

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>
单击以添加到阵列

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>

显示变量

<head>
    <h1>Pushing into an array</h1>
</head>
    <body>

    <p>Enter your input to the form before adding</p>

    <p>Click to add to array</p>
    <p id="demo"></p>

    <br>

    <button onclick="myFunction()">Display Variable</button>

    <form name="myform"> 
    <input type="text" name="text1" value=""> 
    <input type="button" value="Enter Value" onclick="setValue()"> 
    </form>  

    <p id="value"></p>

</body>

这是实际测试的代码吗?什么是“演示”和“myFunction”元素?