2015-10-14 24 views
0

爲了我的Javascript任務,我必須製作一臺老虎機。給文本區添加一個變量JavaScript

我無法得到的一個部分是將我的totalCredits變量添加到我的文本字段中。

它只是將變量放在積分旁邊而不是添加它們。 (要加30 + 2,但我得到302)

我試圖parseFloat和parseInt函數既給我 「的ReferenceError:無效的分配左側」

這是函數

function checkForWin() { 

    if (bet1 == true && symbols[rng1].color == symbols[rng2].color && symbols[rng2].color == 
     symbols[rng3].color && symbols[rng1].color == symbols[rng3].color) { 

document.getElementById("message").innerHTML = "Same Color You Win 2 Credits"; 
      totalCredits = totalCredits + 2; 

      parseInt(document.getElementById('credits').value) = totalCredits; 
      console.log(totalCredits); 

    }; 

代碼:https://jsfiddle.net/Socha/ndLn73bw/

+0

這是什麼線做? ''parseInt(document.getElementById('credits')。value)= totalCredits;' –

+0

像這樣'totalCredits = parseInt(totalCredits)+ 2;'和'document.getElementById('credits')。value = totalCredits;'' –

+0

您不能通過將變量放在左側來重新聲明變量。你已經解決了你自己的問題。 –

回答

1

其更改如下

  total = parseInt(totalCredits) + 2; 

      document.getElementById('credits').value = total; 
+0

這工作!非常感謝Ajmal。 – Matt