我有以下代码,它将数字解释为字符串并将它们相加,例如 100300 而不是 400:
var rate = document.getElementById('pur_price').value;
var pprice = document.getElementById('pur_price').value;
var rate1 = document.getElementById('select2').value;
var refurb = 300;
document.getElementById('avg_claim_rate').value=id;
document.getElementById('amount_claim').value=pprice+refurb;
这是 pprice 和 refurb 字段,因此如果 pprice=100 且 refurb=300,我预计它会显示 400,而目前它显示 100300
最佳答案
您可以使用parseInt
函数:
var pprice = parseInt(document.getElementById('pur_price').value, , 10);
var rate1 = parseInt(document.getElementById('select2').value, , 10);
关于Javascript 将数字添加为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26866468/