我要求输入球队名称,因为我试图创建一个程序来创建基于得分的表格。我必须用JS编写输入代码,因为它在OnClick函数中。但是,我不了解如何存储用户输入(以变量或其他方式存储)。
我尝试了将实际document.write存储在变量中的方法。
<!DOCTYPE html>
<html>
<body>
<h1>Soccer Table Generator</h1>
<h3>How Many Teams Would You Like?</h3>
<select id="num_of_teams">
<option value="Select" selected>Select</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<script>
var menu = document.getElementById("num_of_teams");
menu.addEventListener("change", generateData);
function generateData(event) {
if (menu.value == '2') {
alert("You have selected 2 teams. Would you like to proceed?");
document.write ("What is the name of your first team?");
var teamone = document.write ("<input type = 'text' value =
'input here'<input>");
document.write ("What is the name of your second team?");
} else if (menu.value == '3') {
alert("You have selected 3 teams. Would you like to proceed?");
} else if (menu.value == '4') {
alert("You have selected 4 teams. Would you like to proceed?");
}
}
</script>
</body>
</html>
我希望将输入存储在变量中并打印(当我使用要打印的代码时),但是当我尝试将其显示为未定义时。
答案 0 :(得分:1)
如果您想要一种简单的方式来获取用户输入,可以使用Window.prompt
。例如:
let first_team = prompt("What is the name of your first team?");