【发布时间】:2021-03-26 11:00:33
【问题描述】:
我正在尝试检查是否所有输入字段都填写在此表单中,输入是根据玩家数量创建的,所以我在 JavaScript 中动态创建了它们,我该怎么做? 这是我尝试过的
const x = localStorage.getItem('playersNum');
for (let i = 0; i < x; i++) {
const newInput = document.createElement("INPUT");
newInput.setAttribute("type", "text");
newInput.setAttribute("class", "form-control");
newInput.setAttribute("id", `player${i}`);
newInput.setAttribute("placeholder", "Player's Name");
const parentDiv = document.getElementById('player-list');
parentDiv.appendChild(newInput);
const input = document.getElementById(`player${i}`);
const btn = document.getElementById('startGame');
btn.addEventListener('click', function() {
if (input === "") {
alert('Please fill in the players names');
}
});
}
【问题讨论】:
-
这里有一个问题:
if (input.value === ""),但除了这个问题之外,您在控制台上还会遇到什么错误? -
请提供完整的代码!
x这里是什么? -
@FabrizioCalderanlovestrees 我尝试了 input.value,现在错误不断根据玩家数量重新加载次数,我认为这是因为我在 for 循环中执行此操作。
标签: javascript html css twitter-bootstrap