我不知道如何将来自 document.write 的输入存储在变量中
I don't know how to store input from document.write in a variable
我要求输入团队名称,因为我正在尝试创建一个程序,该程序将根据分数创建 table。我必须用 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>
我希望输入存储在一个变量中并打印(当我使用代码打印它时),但是当我尝试它时它显示未定义。
如果您想要一种获取用户输入的简单方法,您可以使用Window.prompt
。例如:
let first_team = prompt("What is the name of your first team?");
我要求输入团队名称,因为我正在尝试创建一个程序,该程序将根据分数创建 table。我必须用 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>
我希望输入存储在一个变量中并打印(当我使用代码打印它时),但是当我尝试它时它显示未定义。
如果您想要一种获取用户输入的简单方法,您可以使用Window.prompt
。例如:
let first_team = prompt("What is the name of your first team?");