2017-07-22 41 views
0

我想總結一些價格,並把它們放在所有的價格輸入中,但是函數不會對數字求和,它會把數字彼此相鄰。是否有任何代碼將字符串更改爲數字?所有的價格並沒有總結價格,而是把價格彼此相鄰

<html> 

<label class="fa">ext modeling price</label> 
<input type="number" class="form-control" onchange="allPrc();" id="model- 
ext-price" placeholder= "0" ></input> 
<br> 
<label class="fa">int modeling price</label> 
<input type="number" class="form-control" onchange="allPrc();" id="model- 
int-price" placeholder= "0" ></input> 
<hr class="hrline"> 

<label class="fa"> ext renderign price</label> 
<input type="number" class="form-control" onchange="allPrc();" id="render- 
ext-price" placeholder= "0" ></input> 
<br> 
<label class="fa">int rendering price </label> 
<input type="number" class="form-control" onchange="allPrc();" id="render- 
int-price" placeholder= "0" y></input> 
<hr class="hrline"> 
<label class="fa">pproduction price </label> 
<input type="number" class="form-control" onchange="allPrc();" 
id="pproduction-price" placeholder= "0" y></input> 
<hr class="hrline"> 
<label class="fa"> All price </label> 
<input type="number" class="form-control" id="All-price" placeholder= "0" 
readonly></input> 
    </html> 


     <script> 

    document.getElementById ('model-ext-price').onchange = function() {allPrc();}; 
    document.getElementById ('model-int-price').onchange = function() {allPrc();}; 
    document.getElementById ('render-ext-price').onchange = function() {allPrc();}; 
    document.getElementById ('render-int-price').onchange = function() {allPrc();}; 
    document.getElementById ('pproduction-price').onchange = function() {allPrc();}; 





var allPrc = function() { 
var Totalextmdl = document.getElementById ('model-ext-price').value, 
    Totalintmdl = document.getElementById ('model-int-price').value, 
    Totalextrndr = document.getElementById ('render-ext-price').value, 
    Totalintrndr = document.getElementById ('render-int-price').value, 
    Totalpp = document.getElementById ('pproduction-price').value, 
    TotalPrc = 0; 

document.getElementById('All-price').value = TotalPrc; 

    var TotalPrc = Totalextmdl + Totalintmdl + Totalextrndr + Totalintrndr + 
Totalpp; 

    document.getElementById('All-price').value = (TotalPrc>0 && 
    TotalPrc!=='NaN')?TotalPrc:0; 

}; 

    </script> 
+0

,因爲JS認爲,這是一個字符串,所以加號只是基本上串聯字符串。你需要解析浮點數:'''parseFloat(Totalextmdl = document.getElementById('model-ext-price')。value)''' – Treast

回答

0

element.value返回一個字符串。你想要數字。一個簡單的方法來轉換你的域值是使用一元+當你第一次接他們,就像這樣:

var Totalextmdl = +document.getElementById('model-ext-price').value, 
    Totalintmdl = +document.getElementById('model-int-price').value, 
    Totalextrndr = +document.getElementById('render-ext-price').value, 
    Totalintrndr = +document.getElementById('render-int-price').value, 
    Totalpp = +document.getElementById('pproduction-price').value, 
    TotalPrc = 0; 
0

這可能是在var TotalPrc = Totalextmdl + Totalintmdl + Totalextrndr + Totalintrndr + Totalpp;聲明,TotalextmdlTotalintmdlTotalextrndrTotalpp值被解讀爲字符串。

您可以通過將+符號在這些變量前面的值轉換成數字:

var TotalPrc = (+Totalextmdl) + (+Totalintmdl) + (+Totalextrndr) + (+Totalintrndr) + (+Totalpp); 
1

我認爲你正在尋找parseIntparseFloat(取決於如果您有整數與否)

ex:parseInt(document.getElementById ('model-ext-price').value)parseFloat(document.getElementById ('model-ext-price').value)

這可以確保您找回數字而不是字符串。如果parseInt(或parseFloat)失敗,可能希望編寫其他邏輯來執行某些操作。

https://www.w3schools.com/jsref/jsref_parseint.asp https://www.w3schools.com/jsref/jsref_parsefloat.asp

0

將您的字符串值,以數字:

var allPrc = function() { 
var ids =['model-ext-price','model-int-price', 
    'render-ext-price', 'render-int-price', 'pproduction-price']; 

var TotalPrice = 0; 
ids.forEach(funciton(id) { 
     totalPrice+=Number(document.getElementById(id).value; 
    }); 
return TotalPrice; 
} 

document.getElementById('All-price').value = allPrc;