我想總結一些價格,並把它們放在所有的價格輸入中,但是函數不會對數字求和,它會把數字彼此相鄰。是否有任何代碼將字符串更改爲數字?所有的價格並沒有總結價格,而是把價格彼此相鄰
<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>
,因爲JS認爲,這是一個字符串,所以加號只是基本上串聯字符串。你需要解析浮點數:'''parseFloat(Totalextmdl = document.getElementById('model-ext-price')。value)''' – Treast