如何添加va
我试图创建一个系统,每次将输入添加到文本框中时,我都可以创建一个数组,使其不在我的代码中。如何添加va
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;
}
我不知道如何使输入的变量,以及如何使该变量名的二维数组。
回答:
考虑到您的情况,更好的选择是将团队信息存储为对象,然后继续将其推入数组中。当你想访问团队时,这将很容易进行迭代。参考与您的代码相同的代码,可以在'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>
以上是 如何添加va 的全部内容, 来源链接: utcz.com/qa/260439.html