2016-12-26 59 views
2

我的alert("No money")工作正常,但我的alert("works!")不能工作?這些錯誤對我來說沒有任何意義,因爲我覺得我寫的內容在語法上是正確的。爲什麼我的原型不能工作?

感謝您抽出寶貴時間來閱讀:d

這裏是我的gameTime.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>WOMP</title> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="gameTime.css"> 
</head>  
<body> 
    <form class="col-md-4 col-md-offset-4" name="formHandler" id="handle"> 
     <div id="allFields"> 
      <div class="moveUsername"> 
       <h1>(All numbers inputted will be assumed that it's in dollars)</h1> 
       <label for="usr">What's your annual salary?</label> 
       <input type="field" class="form-control" id="salary" placeholder="What's your annual salary?" required="required"> 
      </div> 

      <div class="ageMovement"> 
       <label for="usr">How much do you spend every month on bills?</label> 
       <input type="field" class="form-control" id="monthlyBills" name="ageChecker" placeholder="How much do you spend every month on bills?" required="required"> 
      </div> 

      <div class="emailMovement"> 
       <label for="usr">How much do you spend when going out?</label> 
       <input type="field" class="form-control" id="goingOut" name="emailChecker" placeholder="How much do you spend when going out?" required="required"> 
      </div> 
     </div> 
     <button type="submit" class="btn btn-default" onclick="fin.isSalaryZeroOrLess()">Submit</button> 
    </form> 
    <script type="text/javascript" src="gameTime.js"></script> 
</body> 
</html> 

這裏是我的gameTime.js

function Finance(salary, fixedExpense, variableExpense) { 
    this.salary = salary; 
    this.fixedExpense = fixedExpense; 
    this.variableExpense = variableExpense; 
    this.isSalaryZeroOrLess = function() { 
     var s = parseInt(document.getElementById("salary").value); 

     if(s <= 0) { 
      alert("No money"); 
     } 
    } 
} 



Finance.prototype.greaterThan = function() { 
    var s = parseInt(document.getElementById("salary").value); 
    var userSalary = s/12; 

    if(userSalary < 30000) { 
     alert("works!"); 
    } 
} 

    var fin = new Finance(1000,1000,1000); 

下面是錯誤:

Uncaught TypeError: Cannot set property 'greaterThan' of undefined 
    at gameTime.js:14 
gameTime.html:28 Uncaught TypeError: Cannot read property 'isSalaryZeroOrLess' of undefined 
    at HTMLButtonElement.onclick (gameTime.html:28) 
+0

無法重現的第一個錯誤。有'''Finance.prototype.greaterThan = function(){'缺少。 – Xufox

+0

@Xufox在發佈這個問題後,我發現了這個問題。我修復了它,但它仍然不會工作:( – chompy

+0

@ Xufox當我發現錯誤並修復它時,我檢查了我的控制檯是否有其他錯誤,沒有任何顯示。控制檯中沒有錯誤,但是我的'greaterThan ()'prototype will not work。 – chompy

回答

1

至於評論,你得到的錯誤:

gameTime.html:28 Uncaught TypeError: Cannot read property 'isSalaryZeroOrLess' of undefined at HTMLButtonElement.onclick (gameTime.html:28)

,因爲你想在HTML

<button type="submit" class="btn btn-default" onclick="fin.isSalaryZeroOrLess()">Submit</button> 
</form> 
<script type="text/javascript" src="gameTime.js"></script> 

綁定事件,但你看,你的gameTime.js尚未加載。所以fin還沒有定義。

快速的解決方案:

移動你的腳本標記頭。

強大的解決

Move事件偵聽器JS文件,並使用.addEventListener,敷在功能(IIFE)你的代碼,以避免污染全局範圍。


我已經更新了你的代碼,並以其優良的工作:JSFiddle

+0

非常感謝你抽出時間來做到這一點:)但現在我收到寫着'遺漏的類型錯誤的錯誤:在gameTime.js無法讀取空 的特性「的addEventListener」:31 在gameTime.js:33 ' – chompy

+0

如果你注意到了,我已經添加按鈕的ID,並用它獲取。您可以使用任何機制來獲取元素 – Rajesh

0

你沒有前夕r調用greaterThan函數。你只定義它並附加到財務原型,我沒有看到你曾經用它做過什麼。

+0

如果你的意思是這樣,就像這樣; 'GREATERTHAN();'。我已經試過了,它不起作用。我如何調用它? – chompy

+0

只需將做類似Finance.prototype.greatherThan(); –

+0

現在我不知道你什麼時候能做到這一點,即其綁定到某些事件。我現在甚至都沒有想到。您也越來越bacause鰭的第一個錯誤是不是在當時定義要綁定isSalaryZeroOrLess()函數的onclick。把你的腳本放在頭上,它應該沒問題。這是非常糟糕的做法,你不應該習慣這樣寫js。 –