2013-11-25 108 views
2

需要這個簡單的代碼的一些幫助,如果我們將輸入例如3的「工作經驗」的價值和工資應該加上10%的條件的工資「1000」的價值所以結果應該是「1100」,但在我的公式中,它顯示了1000250的結果,我觀察到如果我將符號「+」更改爲「 - 」它正確顯示「900」,我做錯了什麼?JavaScript運營商+不能正常工作

if (age>=3 && age<10) { 
    var increase_1; 
    var salary_2; 
    increase_1=(salary*10)/100; 
    salary_2=salary+increase_1; 

    document.write('<h4>' +'Proceeding from work experience the new salary was increase by 10%:\t'+ +salary_2+ '<\h4>'); 

另外,如果我使用工資一個防守值,如「工資= 1000」;該程序沒有問題工程..等待一些答案,THX

<html> 
    <head> 
    </head>  
    <script>  
     function doStuff() 
     { 
      var nameAge = document.getElementById("ageInput"); 
      var age = nameAge.value; 
      var nameSalary = document.getElementById("salaryInput"); 
      var salary = nameSalary.value;      
      document.write('<h2>' +'Age experience:\t'+ +age+ '<\h2>'); 

      document.write('<h2>' +'Starting salary ($):\t'+ +salary+ '<\h2>'); 

      if (age>=3 && age<10) { 
       var increase_1; 
       var salary_2; 
       increase_1=(salary*10)/100; 
       salary_2=salary+increase_1;        
       document.write('<h4>' +'Proceeding from work experience the new salary was increase by 10%:\t'+ +salary_2+ '<\h4>'); 
         }  
      else if (age>=10 && age<20){ 
       var increase_2; 
       var salary_3; 
       increase_2=(salary*25)/100; 
       salary_3=salary+increase_2; 
       document.write('<h4>' +'Proceeding from work experience the new salary was increase by 25%:\t'+ +salary_3+ '<\h4>'); 
      } 
      else if (age>=20){         
       document.write('<h4>' +'Proceeding from work experience you get a prize:'+ '<\h4>'); 
       document.write('<img src="http://3.bp.blogspot.com/-2T_AGEs19_4/T_c2ERHsJeI/AAAAAAAIK9E/MGAQAa9ppDE/s800/2013-Mercedes-G-Class-AMG-011.jpg">'); 
      } 
      else { 
       document.write('<h4>' +'Proceeding from work experience the salary is:\t' +salary+'<\h4>');  
      } 
     } 
    </script> 
    <h1>Please enter your work experience(years)</h1> 
    <input id="ageInput" type="text"> 
    <h1>Please enter your salary($)</h1> 
    <input id="salaryInput" type="text">  
    <input type="button" value="Submit" onClick="doStuff()">  
</html> 
+2

歡迎的JavaScript。確保您首先使用數字(例如,不是字符串)。 –

+0

將薪水轉換爲int,它看起來像是將它讀作字符串 –

+0

嘗試做100.0而不是100? –

回答

1

將您的輸入解析爲一個int(或者如果您想要小數,則爲float),它將以字符串形式讀取,從而進行連接。

var salary = parseInt(nameSalary.value); 
var age = parseInt(nameAge.value); 
2

JavaScript是看到它是從輸入框作爲文本字符串,而不是數字值的一些值,和簡單的串聯它們。

如果您正在讀取輸入框中的值並希望在方程中使用它,則需要先通過parseInt()運行它。例如

var age = parseInt(nameAge.value, 10); 

或者,如果你想使用十進制值(浮點),您需要通過parseFloat()

var salary = parseFloat(nameSalary.value); 

傳遞的基數(10)的第二個參數parseInt函數(運行它)會阻止使用ECMAScript低於版本5的舊版瀏覽器將以0開頭的數字解釋爲八進制值。

+1

總是將基數傳遞給parseInt。 –

+0

@FelixKling - 好點 - 我已經更新了我的答案。 –

0

要想從表單字段的數值,你必須把它轉換成一個數字,這樣做的最快方法是從該變量減爲零。

salary-=0; 
increase_1=salary+((salary*10)/100); 
salary_2=salary+increase_1; 

你的 「1000」 的工資將導致(INT)1000就是那麼* 10 = 10000,然後/ 100 = 100,加到1000的原始總和,使1100

你可以嘗試使用功能或原型做腿爲你工作,

Number.prototype.percent = function(p){ 
    return ((this-0)*p)/100; 
} 

,那麼你可以簡單地... increase_1=salary+(salary.percent(10));