【发布时间】:2020-07-30 16:53:26
【问题描述】:
我正在开发一个测验应用程序,它会动态创建 2-4 个按钮来回答问题。但是,如果您单击一个答案,您可以继续单击相同的答案或继续单击其他答案。我希望用户能够单击其中一个按钮,但随后无法继续单击。但需要注意的是:当用户单击其中一个按钮时,会创建一个新的“下一步”按钮,并且该按钮仍然需要它的单击事件。
tl;博士 我需要动态创建的按钮只能点击一次,但“下一步”按钮仍然可以点击。
代码:
function renderButtons() {
var answerContainer = document.getElementById("answer-buttons");
answerContainer.innerHTML = "";
for (var i = 0; i < 4; i++) {
var button = document.createElement("button");
button.classList.add("btn");
button.setAttribute("id", "answerBtns");
button.hasAttribute("data-correct");
button.setAttribute("data-correct", questions[count].answers[i].correct);
button.onclick = btnclick;
button.textContent = questions[count].answers[i].text;
answerContainer.appendChild(button);
}
}
// if user clicks on button check if true
function btnclick(e) {
e.preventDefault();
var value = event.target.dataset.correct;
scoreBox.textContent = "Score: " + score;
if (value === "true") {
score += 5;
document.body.style.background = "green";
} else {
document.body.style.background = "red";
}
var next = document.getElementById("answer-buttons");
var nextBtn = document.createElement("button");
nextBtn.classList.add("nextBtn");
nextBtn.textContent = "Next";
next.appendChild(nextBtn);
nextBtn.onclick = nextBtnFx;
如果您想了解我在说什么,可以在这里找到该应用程序: https://andrethetallguy.github.io/Animal-Quiz/
谢谢!!!
【问题讨论】:
-
在添加“下一步”按钮之前禁用按钮...
标签: javascript click