2016-11-11 125 views
0

我想統計在我的網站上打印隨機值的點擊次數或數量。Javascript:按鈕/點擊計數器集成在功能

var count = 0; 
 
var button = document.getElementById("spin-button"); 
 
var display = document.getElementById("displayCount"); 
 

 
button.onclick = function(){ 
 
    count++; 
 
    display.innerHTML = count; 
 
}
<p>The SPIN-button was pressed <span id="displayCount">0</span> times.</p>

的問題是,我的JS-程序停止一部分/功能。我可以設置新的隨機值,也可以計算按鈕計算新值的頻率。它取決於button.onClick的位置,如果它在外或在括號內。但是兩者並不起作用。

function randomClickX() { 
 

 
    var randomX1 = Math.ceil(Math.random() * 10); 
 
    var randomX2 = Math.ceil(Math.random() * 10); 
 
    var randomX3 = Math.ceil(Math.random() * 10); 
 

 
    document.getElementById("randomX1").innerHTML = randomX1; 
 
    document.getElementById("randomX2").innerHTML = randomX2; 
 
    document.getElementById("randomX3").innerHTML = randomX3; 
 

 
    var ausgabe = "Play again"; 
 
    if (randomX1 === randomX2) { 
 
    if(randomX2 === randomX3) { 
 
     var ausgabe = "Jackpot"; 
 
     } 
 
    } 
 

 
    var count = 0; 
 
    var button = document.getElementById("spin-button"); 
 
    var display = document.getElementById("displayCount"); 
 

 

 
    button.onclick = function(){ 
 
     count++; 
 
     display.innerHTML = count; 
 
    } 
 
}

其實button.onclick工作,而是沿着randomClickX功能沒有得到。

我應該把它放在兩個位置?

+0

您是否想在按下按鈕時更新隨機數? – raphael75

回答

1

你可能想這樣的:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title></title> 
</head> 
<body> 

    <p id="randomX1">X</p> 
    <p id="randomX2">X</p> 
    <p id="randomX3">X</p> 
    <p id="msg"></p> 
    <button type="button" name="button" id="spin-button">SPIN</button> 
    <p>The SPIN-button was pressed <span id="displayCount">0</span> times.</p> 

    <script type="text/javascript"> 

    var count = 0; 
    var button = document.getElementById("spin-button"); 
    var display = document.getElementById("displayCount"); 

    button.onclick = function(){ 
     count++; 
     display.innerHTML = count; 
     randomClickX(); 
    } 

    function randomClickX() { 

     var randomX1 = Math.ceil(Math.random() * 10); 
     var randomX2 = Math.ceil(Math.random() * 10); 
     var randomX3 = Math.ceil(Math.random() * 10); 

     document.getElementById("randomX1").innerHTML = randomX1; 
     document.getElementById("randomX2").innerHTML = randomX2; 
     document.getElementById("randomX3").innerHTML = randomX3; 
     var msg = document.getElementById("msg"); 

     var ausgabe = "Play again"; 
     if (randomX1 === randomX2) { 
      if(randomX2 === randomX3) { 
       var ausgabe = "Jackpot"; 
      } 
     } 
     msg.innerHTML = ausgabe; 
    } 

    </script> 

</body> 
</html> 
+0

非常感謝!按我的意願工作。 你能解釋一下_msg_嗎?爲什麼我需要這個? – Jonas

+0

你不需要它我只是用它來顯示「再次播放」或「累積獎金」(默認爲「再次播放」,但如果所有三個隨機值都相同,它將變成「累積獎金」)。您可以使用其他元素來顯示ausgabe,或者您將執行一些其他功能。這部分取決於你。如果你可以檢查答案是正確的,這將是非常有益的:) –

0

我假設,當點擊(或應該)spin-buttonrandomClickX()被調用。在此基礎上我寫的代碼如下所示:

//set up variables 
var count = 0; 
var button = document.getElementById("spin-button"); 
var display = document.getElementById("displayCount"); 

//when button is pressed. run code 
button.onclick = function(){ 
    count++; //add to the total 
    randomClickX() //spin the wheel 
    display.innerHTML = count; //show the total spin count 
} 

function randomClickX() { 
    //generate the random numbers 
    var randomX1 = Math.ceil(Math.random() * 10); 
    var randomX2 = Math.ceil(Math.random() * 10); 
    var randomX3 = Math.ceil(Math.random() * 10); 
    //display the random numbers 
    document.getElementById("randomX1").innerHTML = randomX1; 
    document.getElementById("randomX2").innerHTML = randomX2; 
    document.getElementById("randomX3").innerHTML = randomX3; 
    //if all 3 are the same assign ausgabe to "Jackpot" 
    var ausgabe = "Play again"; 
    if (randomX1 === randomX2) { 
    if(randomX2 === randomX3) { 
     ausgabe = "Jackpot"; 
     } 
    } 
} 

注:

ausgabe是一條走不通的變量。如果您希望它顯示randomClickX()返回ausgabe,並且當您撥打randomClickX()將其結果分配到您希望它顯示的位置。例如winning.innerHTML = randomClickX()

+0

我在幾個小時之前嘗試過 - 沒有/ doensn't工作。 爲什麼在_button.onclick_函數中包含_randomClickX()_? – Jonas