Javascript 所有价格不包括';I don’不要把价格加起来,而是把价格放在一起

Javascript 所有价格不包括';I don’不要把价格加起来,而是把价格放在一起,javascript,input,numbers,sum,Javascript,Input,Numbers,Sum,我想把一些价格加起来,然后把它们放到所有的价格输入中,但是这个函数不求和,而是把数字放在一起。是否有代码将字符串更改为数字 <html> <label class="fa">ext modeling price</label> <input type="number" class="form-control" onchange="allPrc();" id="model- ext-price" placeholder= "0" ></i

我想把一些价格加起来,然后把它们放到所有的价格输入中,但是这个函数不求和,而是把数字放在一起。是否有代码将字符串更改为数字

 <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>

外部建模价格

整数建模价格
外部渲染价格
整数渲染价格
生产价格
全价 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=函数(){ var Totalextmdl=document.getElementById('model-ext-price')。值, Totalintmdl=document.getElementById('model-int-price')。值, TotalExtNDR=document.getElementById('render-ext-price')。值, Totalintrndr=document.getElementById('render-int-price')。值, Totalpp=document.getElementById('PPProduction-price')。值, 总PRC=0; document.getElementById('All-price')。value=TotalPrc; var TotalPrc=Totalextmdl+Totalintmdl+TotalExtNDR+Totalintrndr+ Totalpp; document.getElementById('All-price')。value=(TotalPrc>0&& TotalPrc!=='NaN')?TotalPrc:0; };
var TotalPrc=Totalextmdl+Totalintmdl+Totalextrndr+Totalintrndr+Totalpp语句,
Totalextmdl
Totalintmdl
Totalextrndr
Totalpp
值读取为字符串

您可以通过将
+
符号放在这些变量前面,将它们的值转换为数字:

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

我认为您正在寻找
parseInt
parseFloat
(取决于您是否有整数)

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

这可以确保您返回的是数字而不是字符串。如果parseInt(或parseFloat)失败,可能需要编写其他逻辑来执行某些操作


元素。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;

将字符串值转换为数字:

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;

这是因为JS认为这是一个字符串,所以加号基本上就是concat字符串。您需要将数字解析为浮点数:
parseFloat(Totalextmdl=document.getElementById('model-ext-price').value)