[英]Event listener not working consistently Javascript
I have a web based quiz that loads a new question, with 4 choices and their respective radio buttons next to them. 我有一个基于Web的测验,加载了一个新问题,旁边有4个选择及其相应的单选按钮。 The problem is I have an event listener on the "next" button that triggers a new question and choices be loaded, except that the questions do not load.
问题是我在“下一个”按钮上有一个事件侦听器,它会触发一个新问题并加载选择,但问题不会加载。 I believe it is because the step counter I have resets each time.
我相信这是因为我每次都会重置步数计数器。 Also something weird is that I have it output to console when the event listener fires, and sometimes when I press next it doesn't fire, and other times it does.
还有一个很奇怪的事情是,当事件监听器启动时,我将其输出到控制台,有时当我按下next时,它不会启动,而在其他时候,它会启动。 I can't find a pattern.
我找不到模式。
I have included the relevant code with a link to JSFiddle if you want to see all of it. 如果您想查看所有内容,我会将相关代码包含到JSFiddle的链接中。 Thank you in advance.
先感谢您。 http://jsfiddle.net/d0u6cz7o/
http://jsfiddle.net/d0u6cz7o/
function Quiz() {
this.step = 0;
this.questionSwap = function () {
document.getElementById('question').innerHTML = allQuestions[this.step].question;
document.getElementById('answer0').innerHTML = allQuestions[this.step].choices[0];
document.getElementById('answer1').innerHTML = allQuestions[this.step].choices[1];
document.getElementById('answer2').innerHTML = allQuestions[this.step].choices[2];
document.getElementById('answer3').innerHTML = allQuestions[this.step].choices[3];
};
var quiz = new Quiz();
quiz.questionSwap();
window.addEventListener('load', function() {
document.getElementById("next").addEventListener("click", function () {
console.log("Here");
quiz.questionSwap();
quiz.step++;
console.log("current question is " + quiz.step);
})
});
Change the submit into a button, remove the listener & change the order of the rotation: 将提交更改为按钮,删除侦听器并更改轮换顺序:
<input id="next" type="button" value="Next!">
document.getElementById("next").addEventListener("click", function ()
{
quiz.step++;
quiz.questionSwap();
console.log("current question is " + quiz.step);
})
http://jsfiddle.net/d0u6cz7o/4/ http://jsfiddle.net/d0u6cz7o/4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.