2016-02-27 25 views
0

我一直在试图找出如何在随机数匹配后每次点击按钮时执行增量或不匹配。jquery匹配随机数后增加分数

基本上,如果数字匹配,匹配的计数器会上升。数字不匹配也是一样。

但目前,柜台只会上涨一次。

的jQuery:

$(document).ready(function(){ 
    var a; 
    var b; 
    display(); 
    displayInt(); 

    function displayInt(){ 
     var disInt; 
     disInt = setInterval(display, 2000); 
    }  

    function display() { 

     a = Math.floor((Math.random()*3)+1); 
     $("#number1").html(a); 
     b = Math.floor((Math.random()*3)+1); 
     $("#number2").html(b);  
    } 

    function addScore(){ 
     var correct; 
     var wrong; 
     correct=0; 
     wrong=0; 

     if (a == b){ 
      correct++; 
      $("#correctScore").html(correct); 

     }else{ 

      wrong++; 
      $("#wrongScore").html(wrong);  
     } 
    } 

    $('input[type="button"]').click(function() { 

     a = $("#number2").html(); 
     b = $("#number1").html(); 
     addScore(); 

    });  
}); 

回答

0

correct是在功能addScore地方。每次增加,但每次都设为0。您需要将correctwrong放在该功能之外。

var correct = 0; 
var wrong = 0; 

function addScore(){ 
    if (a == b){ 
     correct++; 
     $("#correctScore").html(correct); 
    }else{ 
     wrong++; 
     $("#wrongScore").html(wrong);  
    } 
} 
+0

我现在感觉很愚蠢的,太感谢你了! – Yatlax

+0

没问题!请标记最能回答您问题的答案。 – user707727

0
$(document).ready(function(){ 
    var a; 
    var b; 
    var correct = 0; 
    var wrong = 0; 

    display(); 
    displayInt(); 

    function displayInt() { 
     var disInt; 
     disInt = setInterval(display, 2000); 
    }  

    function display() { 
     a = Math.floor((Math.random()*3)+1); 
     $("#number1").html(a); 
     b = Math.floor((Math.random()*3)+1); 
     $("#number2").html(b);  
    } 

    function addScore() { 

     if (a == b) { 
      correct++; 
      $("#correctScore").html(correct); 

     } else { 
      wrong++; 
      $("#wrongScore").html(wrong);  
     } 
    } 

    $('input[type="button"]').click(function() { 
     a = $("#number2").html(); 
     b = $("#number1").html(); 
     addScore(); 

    });  
});