简体   繁体   English

在jquery.click函数之外使用变量?

[英]Using variable outside of jquery.click function?

I want to call a local variable, totalYes, outside of the jquery .click function. 我想在jquery .click函数之外调用局部变量totalYes。 To do this I should make it a global variable, right? 为此,我应该将其设置为全局变量,对吗? Originally, the code looked like: 最初,代码如下所示:

var answers = [thing1, thing2, thing3, thing4, thing5, thing6, thing7, thing8, thing9, thing10, thing11, thing12];

var answers2 = [answers2array12items];

$("#submmit").click(function() {
    var totalYes=0;
    function checkAnswers() {
        for(var i=0; i<answers.length; i++) {
            var userAnswer = document.getElementById("b"+i).value.toLowerCase();
            if (answers.indexOf(userAnswer.toLowerCase()) !== -1 || answers2.indexOf(userAnswer.toLowerCase()) !== -1) {
                totalYes++;
                $("#correcto").show();
            } else {
                $("#incorrecto").show();
            }
        }
    }
    checkAnswers();
    alert(totalYes);
});

And it works fine, however, I want to use it in: 它工作正常,但是,我想在以下地方使用它:

$("total").click(function(){
    alert(totalYes);
});

So I took totalYes, and make it "global", outside the function. 因此,我在函数外将totalYes设为“ global”。 The new version is: 新版本是:

var answers = [thing1, thing2, thing3, thing4, thing5, thing6, thing7, thing8, thing9, thing10, thing11, thing12];

var answers2 = [answers2array12items];

var totalYes = 0;

$("#submmit").click(function() {
    function checkAnswers() {
        for(var i=0; i<answers.length; i++) {
            var userAnswer = document.getElementById("b"+i).value.toLowerCase();
            if (answers.indexOf(userAnswer.toLowerCase()) !== -1 || answers2.indexOf(userAnswer.toLowerCase()) !== -1) {
                totalYes++;
                $("#correcto").show();
            } else {
                $("#incorrecto").show();
            }
        }
    }
    checkAnswers();
    alert(totalYes);
});

But in the new code, instead of adding 1 to totalYes for every correct answer, and increasing totalYes like this: "1,2,3,4,5,6,7,8,9,10,11,12", it increases as: "1,3,6,10,15,21,27,33,39,46,54". 但是在新代码中,不是为每个正确答案在totalYes上加1,而是像这样增加totalYes:“ 1,2,3,4,5,6,7,8,9,10,11,12”,增加为:“ 1、3、6、10、15、21、27、33、39、46、54”。 I have tried changing the totalYes modifier inside of checkAnswers(), from totalYes++; 我试过从totalYes ++更改checkAnswers()内部的totalYes修饰符; to totalYes+=1;, but the problem persists. 到totalYes + = 1 ;,但问题仍然存在。

Also, I would like to know, why the alert box shows up twice every time, instead of just once? 另外,我想知道,为什么警报框每次都显示两次,而不是一次?

edit: here's the html and css for #total and #submmit: 编辑:这是#total和#submmit的html和CSS:

<div class="nextsa1" id="total"><strong>TOTAL</strong></div>

<input type="button" id="submmit" value="GO">

Your code Appears to search the complete answer List each Time it is called upon the user entering a new answer. 您的代码似乎在用户每次输入新答案时都被调用时搜索完整的答案列表。 Therefore, on each invocation the Test Inside your Click handler is successful for the current and all previous answers, which explains the Delta pattern of the totalYes variable. 因此,在每次调用时,对于当前和所有先前的答案,“单击内部测试”处理程序都会成功,这说明了totalYes变量的Delta模式。

Remedy: init totalYes to 0 as the first Instruction inside the Click Handler. 补救措施:init totalYes为0,作为Click Handler内的第一条指令。

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

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