简体   繁体   English

jQuery onclick调用函数

[英]jQuery onclick call function

I've found a script that converts json file into quiz using jquery. 我找到了一个使用jquery将json文件转换为测验的脚本

I am playing with it's code for almost a day now and I can't come with what I wanted to have. 我正在使用它的代码将近一天,而我无法满足我的需求。

Functions quiz().init(); 函数quiz().init(); and quiz().bindSubmit(); quiz().bindSubmit(); are called when page loaded. 在页面加载时被调用。

What I want is the START button must be clicked first to load the Quiz. 我要的是必须首先单击“ 开始”按钮才能加载测验。

$("#start").click(function(){
    currentQuestion = 0;
    questionCount = 0;
    answerArray = [];
    infoMode = false;
    gotData = false;
    inMemoryData = [];
    quiz().init();
    quiz().bindSubmit();
});

HTML: HTML:

<button type="button" id="start">Start</button>
<div id="quiz-content"></div>

It works at first click of START button also in the next clicks, it successfully reset the quiz and goes back to #1. 它在第一次单击“ 开始”按钮时也可以在下次单击时起作用,它成功重置了测验,并返回到#1。

But the problem is after the first click of Start Button , the quiz won't work normally when submitting the quiz. 但是问题是第一次单击“ 开始”按钮后 ,提交测验时测验将无法正常进行。 The quiz began to stucked in #1. 测验开始陷入第一。

For better understanding, JSfiddle here . 为了更好地理解, 请在此处使用JSfiddle

Edited: Basically when the user click start button more than once,the quiz gets started from the beginning ,but didn't get to the next question(Gets stuck on the 1st question itself) 编辑:基本上,当用户多次单击“开始”按钮时,测验从头开始,但是没有到达下一个问题(卡在第一个问题本身上)

When you call bindSubmit function, inside it you are attaching to the submit event of the #quizForm . 当你调用bindSubmit功能,里面要附加到submit的事件#quizForm So when you press Start button twice, there two event handlers attached to the same event and that is because it is not behaving as it should be. 因此,当您按开始按钮两次时,有两个事件处理程序附加到同一事件,这是因为它的行为不正常。 So inside the bindSubmit function you need always disconnect all submit handlers ( $(document).off('submit'); ), like this: 因此,在bindSubmit函数内部,您始终需要断开所有submit处理程序( $(document).off('submit'); ),如下所示:

var bindSubmit = function () {
  $(document).off('submit');
  $(document).on('submit', '#quizForm', function (event) {
    event.preventDefault();
    next(this);
    quiz().init();
  });
};

Here is your original fiddle with mentioned update https://jsfiddle.net/t4p8x02b/35/ 这是您提到的更新的原始提琴https://jsfiddle.net/t4p8x02b/35/

There are couple of things i observed in your code, which needs to be rectified for better management of code: 我在您的代码中观察到了几件事,为了更好地管理代码,需要对其进行纠正:

  • There is no need to expose init() outside your quiz library, for first time initialization, you can call init() before your return from the library(end of quiz() module code). 无需在quiz库外公开init() ,对于首次初始化,您可以在从库返回之前(在quiz()模块代码的结尾init()调用init() )。
  • Also exposing init() makes your quiz() module vulnerable since it can be modified by any external program which could spoil your entire quiz() logic. 另外,暴露init()会使您的quiz()模块容易受到攻击,因为它可以被任何可能破坏整个quiz()逻辑的外部程序修改。
  • Inside bindSubmit() , you dont need to re-initialize your quiz instance to call init() , rather just call init() (refer below code snippet), your event handler will call it without any error [This is the magic of Closure] . bindSubmit()内部,您无需重新初始化测验实例即可调用init() ,而只需调用init() (请参见下面的代码片段),事件处理程序将调用它而不会出现任何错误[这是Closure的神奇之处]

bindSubmit(): bindSubmit():

    $(document).on('submit', '#quizForm', function (event) {
         event.preventDefault();
         next(this);
         init();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM