提问人:VSNAIR 提问时间:5/10/2021 最后编辑:Daniel A. WhiteVSNAIR 更新时间:5/10/2021 访问量:96
净额未以正确的方式工作。javascript 计算出错 [重复]
Net amount not working in right way. Calculation gone wrong with javascript [duplicate]
问:
我正在尝试制作发票脚本。使用 js 执行的计算部分。手动输入总金额和增值税百分比。增值税金额显示正常。但净额添加并不完美。代码如下。
JS系列
$(document).on("change keyup blur", "#caVat", function() {
var gross = $('#caGross').val();
var vat = $('#caVat').val();
var dec = (vat / 100).toFixed(2); //its convert 10 into 0.10
var mult = gross * dec; // gives the value for subtract from main value
var discont = gross - mult;
var add = gross + mult;
$('#caVatamnt').val(mult);
$('#caNetamnt').val(add);
});
HTML 代码
<div class="form-row" style="padding-top: 10px;">
<div class="col">
<h5>Notes:</h5><textarea class="form-control" rows="8"></textarea>
</div>
<div class="col">
<div class="box-bg">
<div class="form-row text-dark">
<div class="col-xl-5 offset-xl-0 align-self-center">
<h6 class="mb-0 font-sm">Gross amount:</h6>
</div>
<div class="col-xl-7 offset-xl-0 text-right align-self-center">
<div class="form-group mb-1"><input class="form-control form-control-sm font-sm" type="number" name="pogrossamount" id="caGross"></div>
</div>
<div class="col-xl-5 offset-xl-0 align-self-center">
<h6 class="mb-0 font-sm">VAT (%):</h6>
</div>
<div class="col-xl-7 offset-xl-0 text-right align-self-center">
<div class="form-group mb-1"><input class="form-control form-control-sm font-sm" type="text" name="povat" id="caVat" ></div>
</div>
<div class="col-xl-5 offset-xl-0 align-self-center">
<h6 class="mb-0 font-sm">TAX Amount:</h6>
</div>
<div class="col-xl-7 offset-xl-0 text-right align-self-center">
<div class="form-group mb-1"><input class="form-control form-control-sm font-sm" type="text" name="potax" id="caVatamnt" readonly ></div>
</div>
<div class="col-xl-5 offset-xl-0 align-self-center">
<h6 class="mb-0 font-sm">Discount:</h6>
</div>
<div class="col-xl-7 offset-xl-0 text-right align-self-center">
<div class="form-group mb-1"><input class="form-control form-control-sm font-sm" type="number" name="podiscount" ></div>
</div>
<div class="col-xl-5 offset-xl-0 align-self-center">
<h6 class="mb-0 font-sm">Net amount:</h6>
</div>
<div class="col-xl-7 offset-xl-0 text-right align-self-center">
<div class="form-group mb-1"><input class="form-control form-control-sm font-sm" type="number" name="ponetamount" id="caNetamnt" readonly ></div>
</div>
<div class="col text-right"> <input class= "genric-btn success circle arrow" type="submit" name="submit" value="submit"><a class="genric-btn warning circle arrow" role="button" style="padding-top: 6px;margin-top: 5px;margin-left: 10px;color: rgb(255,255,255);background: #032c71;" href="window.print();">Print</a></div>
</div>
</div>
</div>
</div>
</div>
</form>
结果在这里输入图像描述,我得到:
答:
-2赞
pall04
5/10/2021
#1
看起来它将值添加为文本字符串。
编辑:我以前的解决方案是有缺陷的,正确的方法:
要从字符串中生成一个数字,您可以在它前面加上 +:
var add = +gross + +mult;
评论
0赞
adiga
5/10/2021
0 + "1" + "2"
返回。并且仍然是"012"
"1".valueOf()
"1"
0赞
VSNAIR
5/10/2021
感谢大家的支持...我得到了解决方案......var add = +gross + +mult;
评论
.val()
返回一个字符串,而不是一个number