我正在尝试创建一个系统,每次将输入添加到文本框中时,我都可以使用我的代码创建一个数组。
HTML:
<div>
<p onclick = "addTeamName()"></p>
<input type = "text" name = "teamName" value = "Team Name" id = "teamname"><br>
<input type="submit" value="Add Team" onclick = "addTeamName()">
</div>
的javascript:
function addTeamName() {
var teamNameValue = document.getElementById("teamname").value;
}
我不知道如何将输入变为变量以及如何使该变量成为二维数组的名称。
答案 0 :(得分:0)
考虑您的场景,更好的选择是将团队信息存储为Object,然后继续将其推送到Array中。当您想要访问团队时,这将很容易迭代。参考与您相同的代码,可以在'addTeamName'函数中进行一些修改。
<html>
<head>
<script>
var teamCounter=1;
var teamArray=[];
function addTeamName() {
var teamDetails = {};
teamDetails.id = "Team"+teamCounter;
teamDetails.name = document.getElementById("teamname").value;
teamArray.push(teamDetails);
teamCounter++;
// Array storing list of teams
console.log(teamArray);
}
</script>
</head>
<body>
<div>
<p onclick = "addTeamName()"></p>
<input type = "text" name = "teamName" value = "Team Name" id = "teamname"><br>
<input type="submit" value="Add Team" onclick = "addTeamName()">
</div>
</body>
</html>